Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
restore-operator: create service itself
Browse files Browse the repository at this point in the history
  • Loading branch information
hongchaodeng committed Jan 8, 2018
1 parent d529af9 commit a175e99
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
23 changes: 14 additions & 9 deletions cmd/restore-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"context"
"flag"
"fmt"
"os"
"runtime"
"time"
Expand All @@ -38,10 +39,12 @@ import (

var (
namespace string
// This is the address of k8s service to restore operator itself for accessing
// backup HTTP endpoints. For example, "restore-operator:19999"
serviceAddrForSelf string
createCRD bool
createCRD bool
)

const (
serviceNameForMyself = "etcd-restore-operator"
servicePortForMyself = 19999
)

func init() {
Expand All @@ -58,10 +61,6 @@ func main() {
if len(name) == 0 {
logrus.Fatalf("must set env %s", constants.EnvOperatorPodName)
}
serviceAddrForSelf = os.Getenv(constants.EnvRestoreOperatorServiceName)
if len(serviceAddrForSelf) == 0 {
logrus.Fatalf("must set env %s", constants.EnvRestoreOperatorServiceName)
}
id, err := os.Hostname()
if err != nil {
logrus.Fatalf("failed to get hostname: %v", err)
Expand All @@ -73,6 +72,12 @@ func main() {
logrus.Infof("Git SHA: %s", version.GitSHA)

kubecli := k8sutil.MustNewKubeClient()

err = createServiceForMyself(kubecli, name, namespace)
if err != nil {
logrus.Fatalf("create service failed: %+v", err)
}

rl, err := resourcelock.New(
resourcelock.EndpointsResourceLock,
namespace,
Expand Down Expand Up @@ -109,7 +114,7 @@ func createRecorder(kubecli kubernetes.Interface, name, namespace string) record
}

func run(stop <-chan struct{}) {
c := controller.New(createCRD, namespace, serviceAddrForSelf)
c := controller.New(createCRD, namespace, fmt.Sprintf("%s:%d", serviceNameForMyself, servicePortForMyself))
err := c.Start(context.TODO())
if err != nil {
logrus.Fatalf("etcd restore operator stopped with error: %v", err)
Expand Down
54 changes: 54 additions & 0 deletions cmd/restore-operator/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2018 The etcd-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/pkg/errors"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
)

// createServiceForMyself gets restore-operator pod labels, strip away "pod-template-hash",
// and then use it as selector to create a service for current restore-operator.
func createServiceForMyself(kubecli kubernetes.Interface, name, namespace string) error {
pod, err := kubecli.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return errors.WithStack(err)
}
// strip away replicaset-specific label added by deployment.
delete(pod.Labels, "pod-template-hash")
svc := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: serviceNameForMyself,
Namespace: namespace,
Labels: pod.Labels,
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{{
Port: int32(servicePortForMyself),
TargetPort: intstr.FromInt(servicePortForMyself),
Protocol: v1.ProtocolTCP,
}},
Selector: pod.Labels,
},
}
_, err = kubecli.CoreV1().Services(namespace).Create(svc)
if err != nil {
return errors.WithStack(err)
}
return nil
}
6 changes: 0 additions & 6 deletions doc/user/walkthrough/restore-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,14 @@ Note that currently the etcd-restore-operator only supports restoring from backu
```sh
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
etcd-operator-2486363115-ltc17 1/1 Running 0 1h
etcd-restore-operator-4203122180-npn3g 1/1 Running 0 7s

$ kubect get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
etcd-restore-operator 10.3.243.216 <none> 19999/TCP 51s
```

3. Verify that the etcd-restore-operator creates the `EtcdRestore` CRD:

```sh
$ kubectl get crd
NAME KIND
etcdclusters.etcd.database.coreos.com CustomResourceDefinition.v1beta1.apiextensions.k8s.io
etcdrestores.etcd.database.coreos.com CustomResourceDefinition.v1beta1.apiextensions.k8s.io
```

Expand Down
6 changes: 0 additions & 6 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ func (f *Framework) setup() error {
}
logrus.Info("etcd operator created successfully")

err = f.SetupEtcdRestoreOperatorService()
if err != nil {
return err
}
logrus.Info("etcd restore operator pod and service created successfully")

logrus.Info("e2e setup successfully")
return nil
}
Expand Down

0 comments on commit a175e99

Please sign in to comment.