Skip to content

Commit

Permalink
Delete objects in different namespaces (#563)
Browse files Browse the repository at this point in the history
* Delete objects in different namespaces

* no change
  • Loading branch information
emosbaugh committed May 19, 2020
1 parent 29dc5b4 commit 7664310
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
41 changes: 23 additions & 18 deletions kotsadm/operator/pkg/client/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -27,6 +28,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

var metadataAccessor = meta.NewAccessor()

type applyResult struct {
hasErr bool
multiStdout [][]byte
Expand All @@ -44,18 +47,23 @@ func (c *Client) diffAndRemovePreviousManifests(applicationManifests Application
return errors.Wrap(err, "failed to decode manifests")
}

targetNamespace := c.TargetNamespace
if applicationManifests.Namespace != "." {
targetNamespace = applicationManifests.Namespace
}

// we need to find the gvk+names that are present in the previous, but not in the current and then remove them
decodedPreviousStrings := strings.Split(string(decodedPrevious), "\n---\n")
decodedPreviousMap := map[string]string{}
for _, decodedPreviousString := range decodedPreviousStrings {
decodedPreviousMap[GetGVKWithName([]byte(decodedPreviousString))] = decodedPreviousString
decodedPreviousMap[GetGVKWithNameAndNs([]byte(decodedPreviousString), targetNamespace)] = decodedPreviousString
}

// now get the current names
decodedCurrentStrings := strings.Split(string(decodedCurrent), "\n---\n")
decodedCurrentMap := map[string]string{}
for _, decodedCurrentString := range decodedCurrentStrings {
decodedCurrentMap[GetGVKWithName([]byte(decodedCurrentString))] = decodedCurrentString
decodedCurrentMap[GetGVKWithNameAndNs([]byte(decodedCurrentString), targetNamespace)] = decodedCurrentString
}

// now remove anything that's in previous but not in current
Expand All @@ -71,10 +79,6 @@ func (c *Client) diffAndRemovePreviousManifests(applicationManifests Application
// this is pretty raw, and required kubectl... we should
// consider some other options here?
kubernetesApplier := applier.NewKubectl(kubectl, "", "", config)
targetNamespace := c.TargetNamespace
if applicationManifests.Namespace != "." {
targetNamespace = applicationManifests.Namespace
}

allPVCs := make([]string, 0)
for k, oldContents := range decodedPreviousMap {
Expand All @@ -89,13 +93,22 @@ func (c *Client) diffAndRemovePreviousManifests(applicationManifests Application

group := ""
kind := ""
namespace := targetNamespace
name := ""

if obj != nil {
if n, _ := metadataAccessor.Namespace(obj); n != "" {
namespace = n
}
name, _ = metadataAccessor.Name(obj)
}

if obj != nil && gvk != nil {
group = gvk.Group
kind = gvk.Kind
log.Printf("deleting manifest(s): %s/%s/%s", group, kind, getObjectName(obj))
log.Printf("deleting manifest(s): %s/%s/%s", group, kind, name)

pvcs, err := getPVCs(targetNamespace, obj, gvk)
pvcs, err := getPVCs(namespace, obj, gvk)
if err != nil {
return errors.Wrap(err, "failed to list PVCs")
}
Expand All @@ -109,13 +122,13 @@ func (c *Client) diffAndRemovePreviousManifests(applicationManifests Application
wait = false
}

stdout, stderr, err := kubernetesApplier.Remove(targetNamespace, []byte(oldContents), wait)
stdout, stderr, err := kubernetesApplier.Remove(namespace, []byte(oldContents), wait)
if err != nil {
log.Printf("stdout (delete) = %s", stdout)
log.Printf("stderr (delete) = %s", stderr)
log.Printf("error: %s", err.Error())
} else {
log.Printf("manifest(s) deleted: %s/%s/%s", group, kind, getObjectName(obj))
log.Printf("manifest(s) deleted: %s/%s/%s", group, kind, name)
}
}

Expand Down Expand Up @@ -377,14 +390,6 @@ func parseK8sYaml(doc []byte) (k8sruntime.Object, *k8sschema.GroupVersionKind, e
return obj, gvk, err
}

func getObjectName(obj k8sruntime.Object) string {
// TODO: something like...
// if o, ok := obj.(metav1.ObjectMeta); ok {
// return o.Name
// }
return ""
}

func getPVCs(targetNamespace string, obj k8sruntime.Object, gvk *k8sschema.GroupVersionKind) ([]string, error) {
var err error
var pods []*corev1.Pod
Expand Down
12 changes: 7 additions & 5 deletions kotsadm/operator/pkg/client/gvkn.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"crypto/sha256"
"fmt"

"github.com/pkg/errors"
Expand All @@ -24,16 +23,19 @@ type OverlySimpleMetadata struct {
Namespace string `yaml:"namespace"`
}

func GetGVKWithName(content []byte) string {
func GetGVKWithNameAndNs(content []byte, baseNS string) string {
o := OverlySimpleGVKWithName{}

if err := yaml.Unmarshal(content, &o); err != nil {
return ""
}

h := sha256.New()
h.Write([]byte(fmt.Sprintf("%s-%s-%s", o.APIVersion, o.Kind, o.Metadata.Name)))
return fmt.Sprintf("%x", h.Sum(nil))
namespace := baseNS
if o.Metadata.Namespace != "" {
namespace = o.Metadata.Namespace
}

return fmt.Sprintf("%s-%s-%s-%s", o.APIVersion, o.Kind, o.Metadata.Name, namespace)
}

func IsCRD(content []byte) bool {
Expand Down

0 comments on commit 7664310

Please sign in to comment.