Skip to content

Commit

Permalink
start fixing #28 by using patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Wiesmüller committed May 14, 2018
1 parent b5980d5 commit 58b3db3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
46 changes: 30 additions & 16 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ deps:
go get -u github.com/golang/lint/golint
go get -u github.com/kisielk/errcheck
go get -u github.com/golang/dep/cmd/dep
go get -u github.com/bborbe/docker_utils/bin/docker_remote_tag_exists
go get -u github.com/bborbe/docker-utils/cmd/docker-remote-tag-exists
go get -u github.com/haya14busa/goverage
go get -u github.com/schrej/godacov
go get -u github.com/maxbrunsfeld/counterfeiter
Expand Down Expand Up @@ -90,7 +90,7 @@ build:

# run specified tool from code
dev:
@go run -ldflags $(KIT_VERSION) cmd/$(NAME)/*.go \
@go run -ldflags $(KIT_VERSION) cmd/$(NAME)/$(NAME).go \
-dir=$(MANIFEST_DIR) \
-namespaces=$(NAMESPACES) \
-teamvault-config=~/.teamvault-sm.json \
Expand All @@ -115,7 +115,7 @@ version:

# install docker tag check
docker_remote_tag_exists:
@go get github.com/bborbe/docker_utils/bin/docker_remote_tag_exists
@go get github.com/bborbe/docker-utils/cmd/docker-remote-tag-exists

# trigger only if docker tag not present
trigger: docker_remote_tag_exists
Expand Down
27 changes: 24 additions & 3 deletions apply/change_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ package apply

import (
"context"
"encoding/json"
"fmt"

jsonpatch "github.com/evanphx/json-patch"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/seibert-media/dimios/change"
k8s_metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s_unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8s_runtime "k8s.io/apimachinery/pkg/runtime"
k8s_schema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
k8s_discovery "k8s.io/client-go/discovery"
k8s_dynamic "k8s.io/client-go/dynamic"
)
Expand Down Expand Up @@ -91,17 +94,35 @@ func (c *Applier) apply(ctx context.Context, change change.Change) error {
return nil
}
var result *k8s_unstructured.Unstructured
if _, err := resource.Get(obj.GetName(), k8s_metav1.GetOptions{}); err != nil {
if original, err := resource.Get(obj.GetName(), k8s_metav1.GetOptions{}); err != nil {
glog.V(3).Infoln("object not present, creating")
result, err = resource.Create(obj)
if err != nil {
return errors.Wrap(err, "create object failed")
}
} else {
glog.V(3).Infoln("object already present, updating")
result, err = resource.Update(obj)

originalBytes, err := json.Marshal(original.Object)
if err != nil {
return errors.Wrap(err, "unable to marshal original")
}

updatedBytes, err := json.Marshal(obj.Object)
if err != nil {
return errors.Wrap(err, "unable to marshal original")
}

patchBytes, err := jsonpatch.CreateMergePatch(originalBytes, updatedBytes)
if err != nil {
return errors.Wrap(err, "unable to decode patch")
}
fmt.Println("ORIG: ", string(originalBytes))
fmt.Println("NEW: ", string(updatedBytes))
fmt.Println("PATCH: ", string(patchBytes))
result, err = resource.Patch(original.GetName(), types.MergePatchType, patchBytes)
if err != nil {
return errors.Wrap(err, "update object failed")
return errors.Wrap(err, "unable to patch object")
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/dimios/dimios.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

var (
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
kubeconfig = flag.String("kubeconfig", "~/.kube/config", "absolute path to the kubeconfig file")
namespaces = flag.String("namespaces", "", "list of kubernetes namespace separated by comma")
port = flag.Int("port", 8080, "port listen on if webhook is activated")
staging = flag.Bool("staging", false, "staging status")
Expand Down

0 comments on commit 58b3db3

Please sign in to comment.