Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests. #1

Merged
merged 1 commit into from
Oct 14, 2019
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
28 changes: 13 additions & 15 deletions api/v1/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
// +build !ignore_autogenerated

/*
Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file

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.
*/
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.
//
// 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.

// autogenerated by controller-gen object, do not modify manually

Expand Down
50 changes: 1 addition & 49 deletions config/crd/bases/druid.sapcloud.io_etcds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
creationTimestamp: null
name: etcds.druid.sapcloud.io
georgekuruvillak marked this conversation as resolved.
Show resolved Hide resolved
spec:
group: druid.sapcloud.io
Expand Down Expand Up @@ -394,39 +395,26 @@ spec:
properties:
deltaSnapshotMemoryLimit:
type: integer
minimum: 0
#default: 10485760
deltaSnapshotPeriod:
type: string
#default: 60s
pattern: '^[-+]?([0-9]*(\.[0-9]*)?[ns|us|µs|μs|ms|S|m|h]+)+$'
etcdConnectionTimeout:
type: string
#default: 30s
etcdQuotaBytes:
type: integer
#default: 8589934592
fullSnapshotSchedule:
type: string
pattern: '^(\d+|\*)(\/\d+)?(\s+(\d+|\*)(\/\d+)?){4}$'
garbageCollectionPeriod:
type: string
#default: 60s
pattern: '^[-+]?([0-9]*(\.[0-9]*)?[ns|us|µs|μs|ms|S|m|h]+)+$'
garbageCollectionPolicy:
type: string
pattern: (LimitBased|Exponential)
#default: Exponential
imageRepository:
type: string
imageVersion:
type: string
port:
type: integer
#default: 8080
pullPolicy:
type: string
pattern: (Never|Always|IfNotPresent)
resources:
properties:
limits:
Expand All @@ -447,51 +435,31 @@ spec:
snapstoreTempDir:
type: string
required:
- port
- imageRepository
- imageVersion
- fullSnapshotSchedule
- resources
- pullPolicy
- garbageCollectionPolicy
- garbageCollectionPeriod
- etcdQuotaBytes
- etcdConnectionTimeout
- snapstoreTempDir
- deltaSnapshotPeriod
- deltaSnapshotMemoryLimit
type: object
etcd:
properties:
clientPort:
type: integer
#default: 2379
defragmentationSchedule:
type: string
pattern: '^(\d+|\*)(\/\d+)?(\s+(\d+|\*)(\/\d+)?){4}$'
enableTLS:
type: boolean
#default: false
imageRepository:
type: string
imageVersion:
type: string
initialClusterState:
type: string
#default: new
initialClusterToken:
type: string
#default: new
metrics:
type: string
pattern: (extensive|basic)
#default: basic
password:
type: string
pullPolicy:
type: string
#default: IfNotPresent
pattern: (Never|Always|IfNotPresent)
resources:
properties:
limits:
Expand All @@ -511,19 +479,12 @@ spec:
type: object
serverPort:
type: integer
#default: 2380
username:
type: string
required:
- defragmentationSchedule
- serverPort
- clientPort
- imageRepository
- imageVersion
- metrics
- resources
- enableTLS
- pullPolicy
- initialClusterToken
- initialClusterState
type: object
Expand All @@ -533,14 +494,10 @@ spec:
type: object
pvcRetentionPolicy:
type: string
pattern: (DeleteAll|RetainMaster|RetRetainAllainMaster|Exponential|LimitBased)
#default: DeleteAll
replicas:
type: integer
#default: 1
storageCapacity:
type: string
#default: "16Gi"
storageClass:
type: string
store:
Expand All @@ -555,7 +512,6 @@ spec:
type: string
required:
- storageContainer
- storePrefix
- storageProvider
- storeSecret
type: object
Expand All @@ -567,12 +523,8 @@ spec:
- etcd
- backup
- store
- pvcRetentionPolicy
- replicas
- storageClass
- tlsServerSecret
- tlsClientSecret
- storageCapacity
type: object
status:
properties:
Expand Down
73 changes: 40 additions & 33 deletions controllers/etcd_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"

druidv1 "github.com/gardener/etcd-druid/api/v1"
"github.com/gardener/etcd-druid/pkg/chartrenderer"
Expand All @@ -33,11 +35,11 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
etcdChartPath = filepath.Join("charts", "etcd")
etcdChartPath = filepath.Join("..", "charts", "etcd")
logger = logrus.New()
)

// FinalizerName is the name of the Plant finalizer.
Expand All @@ -46,12 +48,18 @@ const FinalizerName = "druid.sapcloud.io/etcd-druid"
// EtcdReconciler reconciles a Etcd object
type EtcdReconciler struct {
client.Client
Logger *logrus.Logger
chartApplier kubernetes.ChartApplier
Config *rest.Config
ChartApplier kubernetes.ChartApplier
}

func NewEtcdReconciler(mgr manager.Manager) (*EtcdReconciler, error) {
return (&EtcdReconciler{
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
}).InitializeControllerWithChartApplier()
}

// InitializeChartApplier will use EtcdReconciler client to intialize a Kubernetes client as well as
// a Chart renderer.
func (r *EtcdReconciler) InitializeControllerWithChartApplier() (*EtcdReconciler, error) {
Expand Down Expand Up @@ -91,9 +99,9 @@ func (r *EtcdReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
if !reflect.DeepEqual(etcd.Spec, etcdCopy.Spec) {
etcdCopy.Spec = etcd.Spec
}
r.Logger.Infof("Reconciling etcd: %s", etcd.GetName())
logger.Infof("Reconciling etcd: %s", etcd.GetName())
if !etcdCopy.DeletionTimestamp.IsZero() {
r.Logger.Infof("Deletion timestamp set for etcd: %s", etcd.GetName())
logger.Infof("Deletion timestamp set for etcd: %s", etcd.GetName())
if err := r.removeFinalizersToDependantSecrets(etcdCopy); err != nil {
if err := r.updateEtcdErrorStatus(etcdCopy, err); err != nil {
return ctrl.Result{
Expand All @@ -108,7 +116,7 @@ func (r *EtcdReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}

if sets.NewString(etcd.Finalizers...).Has(FinalizerName) {
r.Logger.Infof("Removing finalizer (%s) from etcd %s", FinalizerName, etcd.GetName())
logger.Infof("Removing finalizer (%s) from etcd %s", FinalizerName, etcd.GetName())
finalizers := sets.NewString(etcdCopy.Finalizers...)
finalizers.Delete(FinalizerName)
etcdCopy.Finalizers = finalizers.UnsortedList()
Expand All @@ -125,30 +133,16 @@ func (r *EtcdReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}, err
}
}
r.Logger.Infof("Deleted etcd %s successfully.", etcd.GetName())
logger.Infof("Deleted etcd %s successfully.", etcd.GetName())
return ctrl.Result{}, nil
}

// Add Finalizers to Etcd
if etcd.DeletionTimestamp.IsZero() {
if finalizers := sets.NewString(etcd.Finalizers...); !finalizers.Has(FinalizerName) {
r.Logger.Infof("Adding finalizer (%s) to etcd %s", FinalizerName, etcd.GetName())
finalizers.Insert(FinalizerName)
etcdCopy.Finalizers = finalizers.UnsortedList()
if err := r.Update(context.TODO(), etcdCopy); err != nil {
if err := r.updateEtcdErrorStatus(etcdCopy, err); err != nil {
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Second * 5,
}, nil
}
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Second * 5,
}, err
}
}
if err := r.addFinalizersToDependantSecrets(etcdCopy); err != nil {
if finalizers := sets.NewString(etcd.Finalizers...); !finalizers.Has(FinalizerName) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need the finalizers?

Choose a reason for hiding this comment

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

Yes. We need it. It's recommended that we have finazliers on the resource being acted on. Basically without finalizer is somebody deletes the secret, etcd-backup-restore pod will fail continuously.

Copy link
Collaborator

Choose a reason for hiding this comment

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

without finalizer is somebody deletes the secret, etcd-backup-restore pod will fail continuously.

This is true for the secrets. But the finalizer above is for the etcd resource. Do we need that?

Choose a reason for hiding this comment

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

Yes. Cause its definitely react on etcd resource. If etcd resource is deleted, then it has to do cleanup job like bring down the etcd sts.

logger.Infof("Adding finalizer (%s) to etcd %s", FinalizerName, etcd.GetName())
finalizers.Insert(FinalizerName)
etcdCopy.Finalizers = finalizers.UnsortedList()
if err := r.Update(context.TODO(), etcdCopy); err != nil {
if err := r.updateEtcdErrorStatus(etcdCopy, err); err != nil {
return ctrl.Result{
Requeue: true,
Expand All @@ -161,6 +155,19 @@ func (r *EtcdReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}, err
}
}
if err := r.addFinalizersToDependantSecrets(etcdCopy); err != nil {
if err := r.updateEtcdErrorStatus(etcdCopy, err); err != nil {
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Second * 5,
}, nil
}
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Second * 5,
}, err
}

etcdValues := map[string]interface{}{
"defragmentationSchedule": etcd.Spec.Etcd.DefragmentationSchedule,
"serverPort": etcd.Spec.Etcd.ServerPort,
Expand Down Expand Up @@ -248,7 +255,7 @@ func (r *EtcdReconciler) addFinalizersToDependantSecrets(etcd *druidv1.Etcd) err
Namespace: etcd.Namespace,
}, &storeSecret)
if finalizers := sets.NewString(storeSecret.Finalizers...); !finalizers.Has(FinalizerName) {
r.Logger.Infof("Adding finalizer (%s) for secret %s", FinalizerName, storeSecret.GetName())
logger.Infof("Adding finalizer (%s) for secret %s", FinalizerName, storeSecret.GetName())
storeSecretCopy := storeSecret.DeepCopy()
finalizers.Insert(FinalizerName)
storeSecretCopy.Finalizers = finalizers.UnsortedList()
Expand All @@ -263,7 +270,7 @@ func (r *EtcdReconciler) addFinalizersToDependantSecrets(etcd *druidv1.Etcd) err
Namespace: etcd.Namespace,
}, &clientSecret)
if finalizers := sets.NewString(clientSecret.Finalizers...); !finalizers.Has(FinalizerName) {
r.Logger.Infof("Adding finalizer (%s) for secret %s", FinalizerName, clientSecret.GetName())
logger.Infof("Adding finalizer (%s) for secret %s", FinalizerName, clientSecret.GetName())
clientSecretCopy := clientSecret.DeepCopy()
finalizers.Insert(FinalizerName)
clientSecretCopy.Finalizers = finalizers.UnsortedList()
Expand All @@ -278,7 +285,7 @@ func (r *EtcdReconciler) addFinalizersToDependantSecrets(etcd *druidv1.Etcd) err
Namespace: etcd.Namespace,
}, &serverSecret)
if finalizers := sets.NewString(serverSecret.Finalizers...); !finalizers.Has(FinalizerName) {
r.Logger.Infof("Adding finalizer (%s) for secret %s", FinalizerName, serverSecret.GetName())
logger.Infof("Adding finalizer (%s) for secret %s", FinalizerName, serverSecret.GetName())
serverSecretCopy := serverSecret.DeepCopy()
finalizers.Insert(FinalizerName)
serverSecretCopy.Finalizers = finalizers.UnsortedList()
Expand All @@ -299,7 +306,7 @@ func (r *EtcdReconciler) removeFinalizersToDependantSecrets(etcd *druidv1.Etcd)
return err
}
if finalizers := sets.NewString(storeSecret.Finalizers...); finalizers.Has(FinalizerName) {
r.Logger.Infof("Removing finalizer (%s) from secret %s", FinalizerName, storeSecret.GetName())
logger.Infof("Removing finalizer (%s) from secret %s", FinalizerName, storeSecret.GetName())
storeSecretCopy := storeSecret.DeepCopy()
finalizers.Delete(FinalizerName)
storeSecretCopy.Finalizers = finalizers.UnsortedList()
Expand All @@ -316,7 +323,7 @@ func (r *EtcdReconciler) removeFinalizersToDependantSecrets(etcd *druidv1.Etcd)
return err
}
if finalizers := sets.NewString(clientSecret.Finalizers...); finalizers.Has(FinalizerName) {
r.Logger.Infof("Removing finalizer (%s) from secret %s", FinalizerName, clientSecret.GetName())
logger.Infof("Removing finalizer (%s) from secret %s", FinalizerName, clientSecret.GetName())
clientSecretCopy := clientSecret.DeepCopy()
finalizers.Delete(FinalizerName)
clientSecretCopy.Finalizers = finalizers.UnsortedList()
Expand All @@ -331,7 +338,7 @@ func (r *EtcdReconciler) removeFinalizersToDependantSecrets(etcd *druidv1.Etcd)
Namespace: etcd.Namespace,
}, &serverSecret)
if finalizers := sets.NewString(serverSecret.Finalizers...); finalizers.Has(FinalizerName) {
r.Logger.Infof("Removing finalizer (%s) from secret %s", FinalizerName, serverSecret.GetName())
logger.Infof("Removing finalizer (%s) from secret %s", FinalizerName, serverSecret.GetName())
serverSecretCopy := serverSecret.DeepCopy()
finalizers.Delete(FinalizerName)
serverSecretCopy.Finalizers = finalizers.UnsortedList()
Expand Down Expand Up @@ -362,7 +369,7 @@ func (r *EtcdReconciler) updateStatusFromServices(etcd *druidv1.Etcd) error {
return err
}
etcd.Status.Endpoints = append(etcd.Status.Endpoints, endpoints)
r.Logger.Infof("etcd endpoints: %v", etcd.Status.Endpoints)
logger.Infof("etcd endpoints: %v", etcd.Status.Endpoints)
return nil
}

Expand Down
Loading