Skip to content

Commit

Permalink
added Special Clean Up function to handle specific outdated obseleted…
Browse files Browse the repository at this point in the history
… object before upgrade
  • Loading branch information
RamLavi committed Dec 11, 2019
1 parent d3c2981 commit 6dc6184
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ func (r *ReconcileNetworkAddonsConfig) renderObjects(networkAddonsConfig *opv1al
}
}

// Clean Up any outdated obsoleted objects
if err := network.SpecialCleanUp(networkAddonsConfig, r.client, objs); err != nil {
log.Printf("failed to Clean Up outdated objects: %v", err)
return objs, err
}

// Generate the objects
objs, err = network.Render(&networkAddonsConfig.Spec, ManifestPath, openshiftNetworkConfig, r.clusterInfo)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/network/image-pull-policy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package network

import (
"context"

"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

opv1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
)
Expand Down Expand Up @@ -40,6 +44,11 @@ func changeSafeImagePullPolicy(prev, next *opv1alpha1.NetworkAddonsConfigSpec) [
return []error{}
}

// Currently not implemented, since there are no obsolete objects under this module
func CleanUpImagePullPolicy(ctx context.Context, client k8sclient.Client, objs []*unstructured.Unstructured) []error {
return nil
}

// Verify if the value is a valid PullPolicy
func verifyPullPolicyType(imagePullPolicy v1.PullPolicy) bool {
switch imagePullPolicy {
Expand Down
7 changes: 7 additions & 0 deletions pkg/network/kubemacpool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package network

import (
"context"
"crypto/rand"
"fmt"
"net"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/kubevirt/cluster-network-addons-operator/pkg/render"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

opv1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
)
Expand Down Expand Up @@ -93,6 +95,11 @@ func changeSafeKubeMacPool(prev, next *opv1alpha1.NetworkAddonsConfigSpec) []err
return []error{}
}

// Currently not implemented, since there are no obsolete objects under this module
func CleanUpKubeMacPool(ctx context.Context, client k8sclient.Client, objs []*unstructured.Unstructured) []error {
return nil
}

// renderLinuxBridge generates the manifests of Linux Bridge
func renderKubeMacPool(conf *opv1alpha1.NetworkAddonsConfigSpec, manifestDir string) ([]*unstructured.Unstructured, error) {
if conf.KubeMacPool == nil {
Expand Down
35 changes: 35 additions & 0 deletions pkg/network/linux-bridge.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package network

import (
"context"
"log"
"os"
"path/filepath"
"reflect"

"github.com/kubevirt/cluster-network-addons-operator/pkg/render"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"

"strings"

apierrors "k8s.io/apimachinery/pkg/api/errors"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

opv1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
"github.com/kubevirt/cluster-network-addons-operator/pkg/network/cni"
Expand All @@ -20,6 +29,32 @@ func changeSafeLinuxBridge(prev, next *opv1alpha1.NetworkAddonsConfigSpec) []err
return nil
}

// In older versions of the operator, we used daemon sets of type 'extensions/v1beta1', later we
// changed that to 'apps/v1'. Because of this change, we are not able to seamlessly upgrade using
// only Update methods. Following this we find and delete the old daemonSet if configured
func CleanUpLinuxBridge(ctx context.Context, client k8sclient.Client, objs []*unstructured.Unstructured) []error {

// Get existing
existing := &unstructured.Unstructured{}
gvk := schema.GroupVersionKind{Group: "extensions", Version: "v1beta1", Kind: "DaemonSet"}
existing.SetGroupVersionKind(gvk)

err := client.Get(ctx, types.NamespacedName{Name: "bridge-marker", Namespace: "linux-bridge"}, existing)
//log.Printf("ram sam sam 3 after get: \nexisting = %v\nerr = %v! ! !", existing, err)
if err == nil || !(apierrors.IsNotFound(err) || strings.Contains(err.Error(), "no matches for kind")) {
log.Printf("ram sam sam 5.0 Found Existing, Deleteing the object")
//Delete the object
err = client.Delete(ctx, existing)
if err != nil {
log.Printf("ram sam sam 5.1 failed Cleaning up Linux-Bridge Object.")
} else {
log.Printf("ram sam sam 5.2 Delete Success, err = %v! ! !", err)
}
return nil
}
return nil
}

// renderLinuxBridge generates the manifests of Linux Bridge
func renderLinuxBridge(conf *opv1alpha1.NetworkAddonsConfigSpec, manifestDir string, clusterInfo *ClusterInfo) ([]*unstructured.Unstructured, error) {
if conf.LinuxBridge == nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/network/multus.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package network

import (
"context"
"os"
"path/filepath"
"reflect"

osv1 "github.com/openshift/api/operator/v1"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

opv1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
"github.com/kubevirt/cluster-network-addons-operator/pkg/network/cni"
Expand Down Expand Up @@ -36,6 +38,11 @@ func changeSafeMultus(prev, next *opv1alpha1.NetworkAddonsConfigSpec) []error {
return nil
}

// Currently not implemented, since there are no obsolete objects under this module
func CleanUpMultus(ctx context.Context, client k8sclient.Client, objs []*unstructured.Unstructured) []error {
return nil
}

// RenderMultus generates the manifests of Multus
func renderMultus(conf *opv1alpha1.NetworkAddonsConfigSpec, manifestDir string, openshiftNetworkConfig *osv1.Network, clusterInfo *ClusterInfo) ([]*unstructured.Unstructured, error) {
if conf.Multus == nil || openshiftNetworkConfig != nil {
Expand Down
24 changes: 24 additions & 0 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package network

import (
"context"
"log"
"reflect"
"strings"

osv1 "github.com/openshift/api/operator/v1"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

opv1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
)
Expand Down Expand Up @@ -50,6 +52,28 @@ func FillDefaults(conf, previous *opv1alpha1.NetworkAddonsConfigSpec) error {
return nil
}

// specialCleanUp checks if there are any outdated objects and deletes them.
// This means that the object wil be installed as new and not by the upgrade kubectl algorithm
// The cleanUp is performed on specific obsoleted objects, so some of the cleanUp functions may be not implemented.
func SpecialCleanUp(networkAddonsConfig *opv1alpha1.NetworkAddonsConfig, client k8sclient.Client, objs []*unstructured.Unstructured) error {

errs := []error{}
ctx := context.TODO()

errs = append(errs, CleanUpMultus(ctx, client, objs)...)
errs = append(errs, CleanUpLinuxBridge(ctx, client, objs)...)
errs = append(errs, CleanUpKubeMacPool(ctx, client, objs)...)
errs = append(errs, CleanUpImagePullPolicy(ctx, client, objs)...)
errs = append(errs, CleanUpNMState(ctx, client, objs)...)
errs = append(errs, CleanUpOvs(ctx, client, objs)...)

if len(errs) > 0 {
return errors.Errorf("invalid configuration:\n%v", errs)
}

return nil
}

// IsChangeSafe checks to see if the change between prev and next are allowed
// FillDefaults and Validate should have been called.
func IsChangeSafe(prev, next *opv1alpha1.NetworkAddonsConfigSpec) error {
Expand Down
7 changes: 7 additions & 0 deletions pkg/network/nmstate.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package network

import (
"context"
"os"
"path/filepath"
"reflect"

"github.com/kubevirt/cluster-network-addons-operator/pkg/render"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

opv1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
)
Expand All @@ -19,6 +21,11 @@ func changeSafeNMState(prev, next *opv1alpha1.NetworkAddonsConfigSpec) []error {
return nil
}

// Currently not implemented, since there are no obsolete objects under this module
func CleanUpNMState(ctx context.Context, client k8sclient.Client, objs []*unstructured.Unstructured) []error {
return nil
}

// renderNMState generates the manifests of NMState handler
func renderNMState(conf *opv1alpha1.NetworkAddonsConfigSpec, manifestDir string, clusterInfo *ClusterInfo) ([]*unstructured.Unstructured, error) {
if conf.NMState == nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/network/ovs.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package network

import (
"context"
"os"
"path/filepath"
"reflect"

"github.com/kubevirt/cluster-network-addons-operator/pkg/render"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"

opv1alpha1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1alpha1"
"github.com/kubevirt/cluster-network-addons-operator/pkg/network/cni"
Expand All @@ -20,6 +22,11 @@ func changeSafeOvs(prev, next *opv1alpha1.NetworkAddonsConfigSpec) []error {
return nil
}

// Currently not implemented, since there are no obsolete objects under this module
func CleanUpOvs(ctx context.Context, client k8sclient.Client, objs []*unstructured.Unstructured) []error {
return nil
}

// renderOvs generates the manifests of Ovs
func renderOvs(conf *opv1alpha1.NetworkAddonsConfigSpec, manifestDir string, clusterInfo *ClusterInfo) ([]*unstructured.Unstructured, error) {
if conf.Ovs == nil {
Expand Down

0 comments on commit 6dc6184

Please sign in to comment.