Merge pull request #2792 from SomtochiAma/flux-diff
Handle multi-doc yaml for flux build
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -250,12 +251,19 @@ func (b *Builder) unMarshallKustomization() (*kustomizev1.Kustomization, error)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to read kustomization file %s: %w", b.kustomizationFile, err)
|
return nil, fmt.Errorf("failed to read kustomization file %s: %w", b.kustomizationFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
k := &kustomizev1.Kustomization{}
|
k := &kustomizev1.Kustomization{}
|
||||||
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data))
|
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data))
|
||||||
err = decoder.Decode(k)
|
// check for kustomization in yaml with the same name and namespace
|
||||||
if err != nil {
|
for !(k.Name == b.name && (k.Namespace == b.namespace || k.Namespace == "")) {
|
||||||
return nil, fmt.Errorf("failed to unmarshall kustomization file %s: %w", b.kustomizationFile, err)
|
err = decoder.Decode(k)
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
return nil, fmt.Errorf("failed find kustomization with name '%s' and namespace '%s' in file '%s'",
|
||||||
|
b.name, b.namespace, b.kustomizationFile)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshall kustomization file %s: %w", b.kustomizationFile, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return k, nil
|
return k, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
@@ -157,3 +158,60 @@ type: kubernetes.io/dockerconfigjson
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_unMarshallKustomization(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
localKsFile string
|
||||||
|
wantErr bool
|
||||||
|
errString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid kustomization",
|
||||||
|
localKsFile: "testdata/local-kustomization/valid.yaml",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Multi-doc yaml containing kustomization and other resources",
|
||||||
|
localKsFile: "testdata/local-kustomization/multi-doc-valid.yaml",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no namespace",
|
||||||
|
localKsFile: "testdata/local-kustomization/no-ns.yaml",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "kustomization with a different name",
|
||||||
|
localKsFile: "testdata/local-kustomization/different-name.yaml",
|
||||||
|
wantErr: true,
|
||||||
|
errString: "failed find kustomization with name",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
b := &Builder{
|
||||||
|
name: "podinfo",
|
||||||
|
namespace: "flux-system",
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
b.kustomizationFile = tt.localKsFile
|
||||||
|
ks, err := b.unMarshallKustomization()
|
||||||
|
if !tt.wantErr {
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected err '%s'", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ks.Name != b.name && ks.Namespace != b.namespace {
|
||||||
|
t.Errorf("expected kustomization '%s/%s' to match '%s/%s'",
|
||||||
|
ks.Name, ks.Namespace, b.name, b.namespace)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error but got nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(err.Error(), tt.errString) {
|
||||||
|
t.Errorf("expected error '%s' to contain string '%s'", err.Error(), tt.errString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
7
internal/build/testdata/local-kustomization/different-name.yaml
vendored
Normal file
7
internal/build/testdata/local-kustomization/different-name.yaml
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
|
||||||
|
kind: Kustomization
|
||||||
|
metadata:
|
||||||
|
name: infra
|
||||||
|
namespace: flux-system
|
||||||
|
spec:
|
||||||
|
path: "./clusters/test-build"
|
||||||
32
internal/build/testdata/local-kustomization/multi-doc-valid.yaml
vendored
Normal file
32
internal/build/testdata/local-kustomization/multi-doc-valid.yaml
vendored
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
---
|
||||||
|
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
|
||||||
|
kind: Kustomization
|
||||||
|
metadata:
|
||||||
|
name: infra-namespace
|
||||||
|
namespace: flux-system
|
||||||
|
labels:
|
||||||
|
component.kutara.io/part-of: definitions
|
||||||
|
spec:
|
||||||
|
path: "./k8s/base/infra"
|
||||||
|
prune: true
|
||||||
|
---
|
||||||
|
---
|
||||||
|
apiVersion: source.toolkit.fluxcd.io/v1beta2
|
||||||
|
kind: GitRepository
|
||||||
|
metadata:
|
||||||
|
name: podinfo
|
||||||
|
namespace: flux-system
|
||||||
|
spec:
|
||||||
|
interval: 30s
|
||||||
|
ref:
|
||||||
|
branch: master
|
||||||
|
url: https://github.com/stefanprodan/podinfo
|
||||||
|
---
|
||||||
|
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
|
||||||
|
kind: Kustomization
|
||||||
|
metadata:
|
||||||
|
name: podinfo
|
||||||
|
namespace: flux-system
|
||||||
|
spec:
|
||||||
|
path: "./clusters/test-build"
|
||||||
|
---
|
||||||
6
internal/build/testdata/local-kustomization/no-ns.yaml
vendored
Normal file
6
internal/build/testdata/local-kustomization/no-ns.yaml
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
|
||||||
|
kind: Kustomization
|
||||||
|
metadata:
|
||||||
|
name: podinfo
|
||||||
|
spec:
|
||||||
|
path: "./clusters/test-build"
|
||||||
7
internal/build/testdata/local-kustomization/valid.yaml
vendored
Normal file
7
internal/build/testdata/local-kustomization/valid.yaml
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
|
||||||
|
kind: Kustomization
|
||||||
|
metadata:
|
||||||
|
name: podinfo
|
||||||
|
namespace: flux-system
|
||||||
|
spec:
|
||||||
|
path: "./clusters/test-build"
|
||||||
Reference in New Issue
Block a user