Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
operator: remove dead code
Browse files Browse the repository at this point in the history
With recent changes deployment CR comparsion code is no more needed.
  • Loading branch information
avalluri committed Oct 18, 2020
1 parent 108daf5 commit 0aa500c
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 263 deletions.
115 changes: 0 additions & 115 deletions pkg/apis/pmemcsi/v1alpha1/deployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package v1alpha1

import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -283,51 +281,6 @@ const (
DeploymentPhaseFailed DeploymentPhase = "Failed"
)

// DeploymentChange type declaration for changes between two deployments
type DeploymentChange int

const (
DriverMode = iota + 1
DriverImage
PullPolicy
LogLevel
ProvisionerImage
NodeRegistrarImage
ControllerResources
NodeResources
NodeSelector
PMEMPercentage
Labels
CACertificate
RegistryCertificate
RegistryKey
NodeControllerCertificate
NodeControllerKey
KubeletDir
)

func (c DeploymentChange) String() string {
return map[DeploymentChange]string{
DriverMode: "deviceMode",
DriverImage: "image",
PullPolicy: "imagePullPolicy",
LogLevel: "logLevel",
ProvisionerImage: "provisionerImage",
NodeRegistrarImage: "nodeRegistrarImage",
ControllerResources: "controllerResources",
NodeResources: "nodeResources",
NodeSelector: "nodeSelector",
PMEMPercentage: "pmemPercentage",
Labels: "labels",
CACertificate: "caCert",
RegistryCertificate: "registryCert",
RegistryKey: "registryKey",
NodeControllerCertificate: "nodeControllerCert",
NodeControllerKey: "nodeControllerKey",
KubeletDir: "kubeletDir",
}[c]
}

func (d *Deployment) SetCondition(t DeploymentConditionType, state corev1.ConditionStatus, reason string) {
for _, c := range d.Status.Conditions {
if c.Type == t {
Expand Down Expand Up @@ -430,74 +383,6 @@ func (d *Deployment) EnsureDefaults(operatorImage string) error {
return nil
}

// Compare compares 'other' deployment spec with current deployment and returns
// the all the changes. If len(changes) == 0 represents both deployment spec
// are equivalent.
func (d *Deployment) Compare(other *Deployment) map[DeploymentChange]struct{} {
changes := map[DeploymentChange]struct{}{}
if d == nil || other == nil {
return changes
}

if d.Spec.DeviceMode != other.Spec.DeviceMode {
changes[DriverMode] = struct{}{}
}
if d.Spec.Image != other.Spec.Image {
changes[DriverImage] = struct{}{}
}
if d.Spec.PullPolicy != other.Spec.PullPolicy {
changes[PullPolicy] = struct{}{}
}
if d.Spec.LogLevel != other.Spec.LogLevel {
changes[LogLevel] = struct{}{}
}
if d.Spec.ProvisionerImage != other.Spec.ProvisionerImage {
changes[ProvisionerImage] = struct{}{}
}
if d.Spec.NodeRegistrarImage != other.Spec.NodeRegistrarImage {
changes[NodeRegistrarImage] = struct{}{}
}
if !compareResources(d.Spec.ControllerResources, other.Spec.ControllerResources) {
changes[ControllerResources] = struct{}{}
}
if !compareResources(d.Spec.NodeResources, other.Spec.NodeResources) {
changes[NodeResources] = struct{}{}
}

if !reflect.DeepEqual(d.Spec.NodeSelector, other.Spec.NodeSelector) {
changes[NodeSelector] = struct{}{}
}

if d.Spec.PMEMPercentage != other.Spec.PMEMPercentage {
changes[PMEMPercentage] = struct{}{}
}

if !reflect.DeepEqual(d.Spec.Labels, other.Spec.Labels) {
changes[Labels] = struct{}{}
}

if bytes.Compare(d.Spec.CACert, other.Spec.CACert) != 0 {
changes[CACertificate] = struct{}{}
}
if bytes.Compare(d.Spec.RegistryCert, other.Spec.RegistryCert) != 0 {
changes[RegistryCertificate] = struct{}{}
}
if bytes.Compare(d.Spec.NodeControllerCert, other.Spec.NodeControllerCert) != 0 {
changes[NodeControllerCertificate] = struct{}{}
}
if bytes.Compare(d.Spec.RegistryPrivateKey, other.Spec.RegistryPrivateKey) != 0 {
changes[RegistryKey] = struct{}{}
}
if bytes.Compare(d.Spec.NodeControllerPrivateKey, other.Spec.NodeControllerPrivateKey) != 0 {
changes[NodeControllerKey] = struct{}{}
}
if d.Spec.KubeletDir != other.Spec.KubeletDir {
changes[KubeletDir] = struct{}{}
}

return changes
}

// GetHyphenedName returns the name of the deployment with dots replaced by hyphens.
// Most objects created for the deployment will use hyphens in the name, sometimes
// with an additional suffix like -controller, but others must use the original
Expand Down
84 changes: 0 additions & 84 deletions pkg/apis/pmemcsi/v1alpha1/deployment_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
api "github.com/intel/pmem-csi/pkg/apis/pmemcsi/v1alpha1"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -110,89 +109,6 @@ spec:
Expect(nrs.Memory().Cmp(resource.MustParse("100Mi"))).Should(BeZero(), "node driver 'cpu' resource mismatch")
})

It("compare two deployments", func() {
d1 := &api.Deployment{}
d2 := &api.Deployment{}
changes := map[api.DeploymentChange]struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "two empty deployments should be equal")

err := d1.EnsureDefaults("")
Expect(err).ShouldNot(HaveOccurred(), "ensure defaults")
err = d2.EnsureDefaults("")
Expect(err).ShouldNot(HaveOccurred(), "ensure defaults")
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "two default deployments should be equaval")

d2.Spec.DeviceMode = api.DeviceModeDirect
changes[api.DriverMode] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect chagned device mode")

d2.Spec.LogLevel = d2.Spec.LogLevel + 1
changes[api.LogLevel] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in log level")

d2.Spec.Image = "new-driver-image"
changes[api.DriverImage] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in driver image")

d2.Spec.PullPolicy = corev1.PullNever
changes[api.PullPolicy] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in image pull policy")

d2.Spec.ProvisionerImage = "new-provisioner-image"
changes[api.ProvisionerImage] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in provisioner image")

d2.Spec.NodeRegistrarImage = "new-node-driver-registrar-image"
changes[api.NodeRegistrarImage] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node registrar image")

d2.Spec.ControllerResources = &corev1.ResourceRequirements{}
changes[api.ControllerResources] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in controller resources")

d2.Spec.ControllerResources = &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("10m"),
corev1.ResourceMemory: resource.MustParse("5Gi"),
},
}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in controller resource requests")

d2.Spec.ControllerResources = &corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("10m"),
corev1.ResourceMemory: resource.MustParse("5Gi"),
},
}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in controller resource limits")

d2.Spec.NodeResources = &corev1.ResourceRequirements{}
changes[api.NodeResources] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node resources")

d2.Spec.NodeResources = &corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("10m"),
corev1.ResourceMemory: resource.MustParse("5Gi"),
},
}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node resource limits")

d2.Spec.NodeResources = &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("10m"),
corev1.ResourceMemory: resource.MustParse("5Gi"),
},
}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node resource requests")

d2.Spec.NodeSelector = map[string]string{
"new-label": "value",
}
changes[api.NodeSelector] = struct{}{}
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node selector")
})

It("should have valid json schema", func() {

crdFile := os.Getenv("REPO_ROOT") + "/deploy/crd/pmem-csi.intel.com_deployments.yaml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
storagev1beta1 "k8s.io/api/storage/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -371,69 +370,6 @@ func (r *ReconcileDeployment) Get(obj runtime.Object) error {
return r.client.Get(context.TODO(), key, obj)
}

// Create create new Kubernetes object
func (r *ReconcileDeployment) Create(obj runtime.Object) error {
metaObj, err := meta.Accessor(obj)
if err != nil {
return fmt.Errorf("internal error %T: %v", obj, err)
}
klog.Infof("Creating: '%s/%s' of type %T", metaObj.GetNamespace(), metaObj.GetName(), obj)
return r.client.Create(context.TODO(), obj)
}

// Update updates existing Kubernetes object. The object must be a modified copy of the existing object in the apiserver.
func (r *ReconcileDeployment) Update(obj runtime.Object) error {
metaObj, err := meta.Accessor(obj)
if err != nil {
return fmt.Errorf("internal error %T: %v", obj, err)
}
klog.Infof("Updating '%s/%s' of type '%T'", metaObj.GetNamespace(), metaObj.GetName(), obj)
return r.client.Update(context.TODO(), obj)
}

// UpdateOrCreate updates the spec of an existing object or, if it does not exist yet, creates it.
func (r *ReconcileDeployment) UpdateOrCreate(obj runtime.Object) error {
metaObj, err := meta.Accessor(obj)
if err != nil {
return fmt.Errorf("internal error %T: %v", obj, err)
}
existing := obj.DeepCopyObject()
err = r.Get(existing)
if err != nil && !errors.IsNotFound(err) {
return err
}
if err == nil {
metaExisting, err := meta.Accessor(existing)
if err != nil {
return fmt.Errorf("internal error %T: %v", existing, err)
}

ownerRef := metaObj.GetOwnerReferences()[0]
if !isOwnedBy(metaExisting, &ownerRef) {
return fmt.Errorf("'%s' of type %T is not owned by '%s'", metaObj.GetName(), obj, ownerRef.Name)
}

// Copy metadata from existing object
metaObj.SetGenerateName(metaExisting.GetGenerateName())
metaObj.SetSelfLink(metaExisting.GetSelfLink())
metaObj.SetUID(metaExisting.GetUID())
metaObj.SetResourceVersion(metaExisting.GetResourceVersion())
metaObj.SetGeneration(metaExisting.GetGeneration())
metaObj.SetCreationTimestamp(metaExisting.GetCreationTimestamp())
metaObj.SetAnnotations(metaExisting.GetAnnotations())
metaObj.SetFinalizers(metaExisting.GetFinalizers())
metaObj.SetClusterName(metaExisting.GetClusterName())
metaObj.SetManagedFields(metaExisting.GetManagedFields())
metaObj.SetLabels(joinMaps(metaExisting.GetLabels(), metaObj.GetLabels()))

klog.Infof("Updating '%s/%s' of type '%T'", metaObj.GetNamespace(), metaObj.GetName(), obj)
return r.client.Update(context.TODO(), obj)
}
// Fall back to creating the object.
klog.Infof("Creating '%s/%s' of type '%T'", metaObj.GetNamespace(), metaObj.GetName(), obj)
return r.client.Create(context.TODO(), obj)
}

// Delete delete existing Kubernetes object
func (r *ReconcileDeployment) Delete(obj runtime.Object) error {
metaObj, err := meta.Accessor(obj)
Expand Down

0 comments on commit 0aa500c

Please sign in to comment.