Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Oct 11, 2018
1 parent 0521ac2 commit e04b4e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
14 changes: 9 additions & 5 deletions cluster/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ type metadata struct {
}

type apiObject struct {
resource.Resource
Kind string `yaml:"kind"`
Metadata metadata `yaml:"metadata"`
OriginalResource resource.Resource
Payload []byte
Kind string `yaml:"kind"`
Metadata metadata `yaml:"metadata"`
}

// A convenience for getting an minimal object from some bytes.
Expand Down Expand Up @@ -216,9 +217,12 @@ func (c *Cluster) Sync(spec cluster.SyncDef) error {
if stage.res == nil {
continue
}
obj, err := parseObj(stage.res.Bytes())

resBytes := stage.res.Bytes()
obj, err := parseObj(resBytes)
if err == nil {
obj.Resource = stage.res
obj.OriginalResource = stage.res
obj.Payload = resBytes
cs.stage(stage.cmd, obj)
} else {
errs = append(errs, cluster.ResourceError{Resource: stage.res, Error: err})
Expand Down
6 changes: 3 additions & 3 deletions cluster/kubernetes/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func (c *Kubectl) apply(logger log.Logger, cs changeSet) (errs cluster.SyncError
args = append(args, cmd)
if err := c.doCommand(logger, makeMultidoc(objs), args...); err != nil {
for _, obj := range objs {
r := bytes.NewReader(obj.Bytes())
r := bytes.NewReader(obj.Payload)
if err := c.doCommand(logger, r, args...); err != nil {
errs = append(errs, cluster.ResourceError{obj.Resource, err})
errs = append(errs, cluster.ResourceError{obj.OriginalResource, err})
}
}
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func makeMultidoc(objs []*apiObject) *bytes.Buffer {
buf := &bytes.Buffer{}
for _, obj := range objs {
buf.WriteString("\n---\n")
buf.Write(obj.Bytes())
buf.Write(obj.Payload)
}
return buf
}
Expand Down
75 changes: 36 additions & 39 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package sync
import (
"crypto/sha1"
"encoding/hex"
"fmt"

"github.com/go-kit/kit/log"
"github.com/imdario/mergo"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"

"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/policy"
Expand All @@ -24,42 +21,42 @@ func getStackChecksum(repoResources map[string]resource.Resource) string {
return hex.EncodeToString(checksum.Sum(nil))
}

func (def *SyncDef) Associate(stackLabel, stackName, checksumAnnotation, checksum string) error {
for _, action := range def.Actions {
// There is no apply action associated with this action
if action.Apply == nil {
continue
}

source := action.Apply.Source()
bytes := action.Apply.Bytes()

// We decode the YAML into a generic map because we don't know its type
definition := make(map[interface{}]interface{})
if err := yaml.Unmarshal(bytes, &definition); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to parse yaml from %s", source))
}
mixin := make(map[interface{}]interface{})
mixinYaml := []byte(fmt.Sprintf(("" +
"metadata:\n" +
" labels:\n" +
" %s: %s\n" +
" annotations:\n" +
" %s: %s\n"),
stackLabel, stackName, checksumAnnotation, checksum,
))
if err := yaml.Unmarshal(mixinYaml, &mixin); err != nil {
return errors.Wrap(err, "failed to parse yaml for mixin")
}
mergo.Merge(&definition, mixin)
newBytes, err := yaml.Marshal(definition)
if err != nil {
return errors.Wrap(err, "failed to serialize yaml after mixing in association")
}
action.Apply.UpdateBytes(newBytes)
}
return nil
}
// func (def *SyncDef) Associate(stackLabel, stackName, checksumAnnotation, checksum string) error {
// for _, action := range def.Actions {
// // There is no apply action associated with this action
// if action.Apply == nil {
// continue
// }

// source := action.Apply.Source()
// bytes := action.Apply.Bytes()

// // We decode the YAML into a generic map because we don't know its type
// definition := make(map[interface{}]interface{})
// if err := yaml.Unmarshal(bytes, &definition); err != nil {
// return errors.Wrap(err, fmt.Sprintf("failed to parse yaml from %s", source))
// }
// mixin := make(map[interface{}]interface{})
// mixinYaml := []byte(fmt.Sprintf(("" +
// "metadata:\n" +
// " labels:\n" +
// " %s: %s\n" +
// " annotations:\n" +
// " %s: %s\n"),
// stackLabel, stackName, checksumAnnotation, checksum,
// ))
// if err := yaml.Unmarshal(mixinYaml, &mixin); err != nil {
// return errors.Wrap(err, "failed to parse yaml for mixin")
// }
// mergo.Merge(&definition, mixin)
// newBytes, err := yaml.Marshal(definition)
// if err != nil {
// return errors.Wrap(err, "failed to serialize yaml after mixing in association")
// }
// action.Apply.UpdateBytes(newBytes)
// }
// return nil
// }

// Sync synchronises the cluster to the files in a directory
func Sync(m cluster.Manifests, repoResources map[string]resource.Resource, clus cluster.Cluster, tracks bool, deletes bool, logger log.Logger) error {
Expand Down

0 comments on commit e04b4e1

Please sign in to comment.