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

restore-operator: create service itself #1837

Merged
merged 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
56 changes: 56 additions & 0 deletions cmd/restore-operator/service.go
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

group this import separately.


"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
}
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
11 changes: 0 additions & 11 deletions example/etcd-restore-operator/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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