Skip to content

Commit

Permalink
detect kotskinds in multi-yaml docs (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh committed Aug 6, 2021
1 parent 70eaea9 commit 7ff3c7f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pkg/base/replicated.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func renderReplicated(u *upstreamtypes.Upstream, renderOptions *RenderOptions) (
// this will prevent errors later from ShouldBeIncludedInBaseKustomization
newContent := [][]byte{}
isKotsKind := false
for _, doc := range convertToSingleDocs(upstreamFile.Content) {
for _, doc := range util.ConvertToSingleDocs(upstreamFile.Content) {
file := BaseFile{Path: upstreamFile.Path, Content: doc}
// ignore the error here, we will catch it later in ShouldBeIncludedInBaseKustomization
if ok, _ := file.IsKotsKind(); ok {
Expand Down
16 changes: 2 additions & 14 deletions pkg/base/write.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package base

import (
"bytes"
"fmt"
"io/ioutil"
"os"
Expand All @@ -11,6 +10,7 @@ import (

"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/util"
"gopkg.in/yaml.v2"
kustomizetypes "sigs.k8s.io/kustomize/api/types"
)
Expand Down Expand Up @@ -321,7 +321,7 @@ func deduplicateOnContent(files []BaseFile, excludeKotsKinds bool, baseNS string
func convertToSingleDocBaseFiles(files []BaseFile) []BaseFile {
singleDocs := []BaseFile{}
for _, file := range files {
docs := convertToSingleDocs(file.Content)
docs := util.ConvertToSingleDocs(file.Content)
// This is here so as not to change previous behavior
if len(docs) == 0 {
singleDocs = append(singleDocs, BaseFile{
Expand All @@ -348,18 +348,6 @@ func convertToSingleDocBaseFiles(files []BaseFile) []BaseFile {
return singleDocs
}

func convertToSingleDocs(doc []byte) [][]byte {
singleDocs := [][]byte{}
docs := bytes.Split(doc, []byte("\n---\n"))
for _, doc := range docs {
if len(bytes.TrimSpace(doc)) == 0 {
continue
}
singleDocs = append(singleDocs, doc)
}
return singleDocs
}

func (b *Base) GetOverlaysDir(options WriteOptions) string {
renderDir := options.BaseDir

Expand Down
87 changes: 46 additions & 41 deletions pkg/kotsutil/kots.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,52 +374,57 @@ func LoadKotsKindsFromPath(fromDir string) (*KotsKinds, error) {
return err
}

decoded, gvk, err := decode(contents, nil, nil)
if err != nil {
// TODO: log something on yaml errors (based on file extention)
return nil // not an error because the file might not be yaml
}
// kots kinds could be part of a multi-yaml doc
docs := util.ConvertToSingleDocs(contents)

if strings.HasPrefix(gvk.String(), "troubleshoot.replicated.com/v1beta1,") {
contents, err = docrewrite.ConvertToV1Beta2(contents)
for _, doc := range docs {
decoded, gvk, err := decode(doc, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to convert to v1beta2")
// TODO: log something on yaml errors (based on file extention)
return nil // not an error because the file might not be yaml
}
decoded, gvk, err = decode(contents, nil, nil)
if err != nil {
return err

if strings.HasPrefix(gvk.String(), "troubleshoot.replicated.com/v1beta1,") {
doc, err = docrewrite.ConvertToV1Beta2(doc)
if err != nil {
return errors.Wrap(err, "failed to convert to v1beta2")
}
decoded, gvk, err = decode(doc, nil, nil)
if err != nil {
return err
}
}
}

switch gvk.String() {
case "kots.io/v1beta1, Kind=Config":
kotsKinds.Config = decoded.(*kotsv1beta1.Config)
case "kots.io/v1beta1, Kind=ConfigValues":
kotsKinds.ConfigValues = decoded.(*kotsv1beta1.ConfigValues)
case "kots.io/v1beta1, Kind=Application":
kotsKinds.KotsApplication = *decoded.(*kotsv1beta1.Application)
case "kots.io/v1beta1, Kind=License":
kotsKinds.License = decoded.(*kotsv1beta1.License)
case "kots.io/v1beta1, Kind=Identity":
kotsKinds.Identity = decoded.(*kotsv1beta1.Identity)
case "kots.io/v1beta1, Kind=IdentityConfig":
kotsKinds.IdentityConfig = decoded.(*kotsv1beta1.IdentityConfig)
case "kots.io/v1beta1, Kind=Installation":
kotsKinds.Installation = *decoded.(*kotsv1beta1.Installation)
case "troubleshoot.sh/v1beta2, Kind=Collector":
kotsKinds.Collector = decoded.(*troubleshootv1beta2.Collector)
case "troubleshoot.sh/v1beta2, Kind=Analyzer":
kotsKinds.Analyzer = decoded.(*troubleshootv1beta2.Analyzer)
case "troubleshoot.sh/v1beta2, Kind=SupportBundle":
kotsKinds.SupportBundle = decoded.(*troubleshootv1beta2.SupportBundle)
case "troubleshoot.sh/v1beta2, Kind=Redactor":
kotsKinds.Redactor = decoded.(*troubleshootv1beta2.Redactor)
case "troubleshoot.sh/v1beta2, Kind=Preflight":
kotsKinds.Preflight = decoded.(*troubleshootv1beta2.Preflight)
case "velero.io/v1, Kind=Backup":
kotsKinds.Backup = decoded.(*velerov1.Backup)
case "app.k8s.io/v1beta1, Kind=Application":
kotsKinds.Application = decoded.(*applicationv1beta1.Application)
switch gvk.String() {
case "kots.io/v1beta1, Kind=Config":
kotsKinds.Config = decoded.(*kotsv1beta1.Config)
case "kots.io/v1beta1, Kind=ConfigValues":
kotsKinds.ConfigValues = decoded.(*kotsv1beta1.ConfigValues)
case "kots.io/v1beta1, Kind=Application":
kotsKinds.KotsApplication = *decoded.(*kotsv1beta1.Application)
case "kots.io/v1beta1, Kind=License":
kotsKinds.License = decoded.(*kotsv1beta1.License)
case "kots.io/v1beta1, Kind=Identity":
kotsKinds.Identity = decoded.(*kotsv1beta1.Identity)
case "kots.io/v1beta1, Kind=IdentityConfig":
kotsKinds.IdentityConfig = decoded.(*kotsv1beta1.IdentityConfig)
case "kots.io/v1beta1, Kind=Installation":
kotsKinds.Installation = *decoded.(*kotsv1beta1.Installation)
case "troubleshoot.sh/v1beta2, Kind=Collector":
kotsKinds.Collector = decoded.(*troubleshootv1beta2.Collector)
case "troubleshoot.sh/v1beta2, Kind=Analyzer":
kotsKinds.Analyzer = decoded.(*troubleshootv1beta2.Analyzer)
case "troubleshoot.sh/v1beta2, Kind=SupportBundle":
kotsKinds.SupportBundle = decoded.(*troubleshootv1beta2.SupportBundle)
case "troubleshoot.sh/v1beta2, Kind=Redactor":
kotsKinds.Redactor = decoded.(*troubleshootv1beta2.Redactor)
case "troubleshoot.sh/v1beta2, Kind=Preflight":
kotsKinds.Preflight = decoded.(*troubleshootv1beta2.Preflight)
case "velero.io/v1, Kind=Backup":
kotsKinds.Backup = decoded.(*velerov1.Backup)
case "app.k8s.io/v1beta1, Kind=Application":
kotsKinds.Application = decoded.(*applicationv1beta1.Application)
}
}

return nil
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ func CompareStringArrays(arr1, arr2 []string) bool {
return true
}

func ConvertToSingleDocs(doc []byte) [][]byte {
singleDocs := [][]byte{}
docs := bytes.Split(doc, []byte("\n---\n"))
for _, doc := range docs {
if len(bytes.TrimSpace(doc)) == 0 {
continue
}
singleDocs = append(singleDocs, doc)
}
return singleDocs
}

type ActionableError struct {
NoRetry bool
Message string
Expand Down

0 comments on commit 7ff3c7f

Please sign in to comment.