From 28e9fcf3ddd6b70dca29923ca1f125dc0b017949 Mon Sep 17 00:00:00 2001 From: Hongchao Deng Date: Tue, 9 Jan 2018 11:24:20 -0800 Subject: [PATCH] restore-operator: create service itself (#1837) --- CHANGELOG.md | 2 + cmd/restore-operator/main.go | 23 +++++--- cmd/restore-operator/service.go | 56 +++++++++++++++++++ doc/user/walkthrough/restore-operator.md | 6 -- example/etcd-restore-operator/deployment.yaml | 11 ---- test/e2e/framework/framework.go | 6 -- 6 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 cmd/restore-operator/service.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 92551504c..492d21f96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Changed +- etcd-restore-operator will create a service for itself as the backup storage proxy. Delete the service in deployment yaml. + ### Removed ### Fixed diff --git a/cmd/restore-operator/main.go b/cmd/restore-operator/main.go index 2e7d669ef..a766e1cb6 100644 --- a/cmd/restore-operator/main.go +++ b/cmd/restore-operator/main.go @@ -17,6 +17,7 @@ package main import ( "context" "flag" + "fmt" "os" "runtime" "time" @@ -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() { @@ -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) @@ -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, @@ -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) diff --git a/cmd/restore-operator/service.go b/cmd/restore-operator/service.go new file mode 100644 index 000000000..e3b08110d --- /dev/null +++ b/cmd/restore-operator/service.go @@ -0,0 +1,56 @@ +// 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/coreos/etcd-operator/pkg/util/k8sutil" + + "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 && !k8sutil.IsKubernetesResourceAlreadyExistError(err) { + return errors.WithStack(err) + } + return nil +} diff --git a/doc/user/walkthrough/restore-operator.md b/doc/user/walkthrough/restore-operator.md index e91fff1bd..b2fc2edf5 100644 --- a/doc/user/walkthrough/restore-operator.md +++ b/doc/user/walkthrough/restore-operator.md @@ -48,12 +48,7 @@ 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 19999/TCP 51s ``` 3. Verify that the etcd-restore-operator creates the `EtcdRestore` CRD: @@ -61,7 +56,6 @@ Note that currently the etcd-restore-operator only supports restoring from backu ```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 ``` diff --git a/example/etcd-restore-operator/deployment.yaml b/example/etcd-restore-operator/deployment.yaml index fd3d4cef4..0d2ec48a3 100644 --- a/example/etcd-restore-operator/deployment.yaml +++ b/example/etcd-restore-operator/deployment.yaml @@ -27,14 +27,3 @@ spec: fieldPath: metadata.name - name: SERVICE_ADDR value: "etcd-restore-operator:19999" ---- -apiVersion: v1 -kind: Service -metadata: - name: etcd-restore-operator -spec: - selector: - name: etcd-restore-operator - ports: - - protocol: TCP - port: 19999 diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index e7210fc96..d67cd7055 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -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 }