Skip to content

Commit

Permalink
Surface HelmChart custom resource decoding and rendering errors better (
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh authored Feb 13, 2023
1 parent a2dbc06 commit db44f1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
18 changes: 2 additions & 16 deletions pkg/base/replicated.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
troubleshootscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
stdyaml "gopkg.in/yaml.v2"
"helm.sh/helm/v3/pkg/chart"
"k8s.io/client-go/kubernetes/scheme"
applicationv1beta1 "sigs.k8s.io/application/api/v1beta1"
Expand Down Expand Up @@ -312,13 +311,13 @@ func upstreamFileToBaseFile(upstreamFile upstreamtypes.UpstreamFile, builder tem
func findAllKotsHelmCharts(upstreamFiles []upstreamtypes.UpstreamFile, builder template.Builder, log *logger.CLILogger) ([]*kotsv1beta1.HelmChart, error) {
kotsHelmCharts := []*kotsv1beta1.HelmChart{}
for _, upstreamFile := range upstreamFiles {
if !isHelmChartKind(upstreamFile.Content) {
if !kotsutil.IsHelmChartKind(upstreamFile.Content) {
continue
}

baseFile, err := upstreamFileToBaseFile(upstreamFile, builder, log)
if err != nil {
continue
return nil, errors.Wrapf(err, "failed to convert upstream file %s to base", upstreamFile.Path)
}

helmChart, err := ParseHelmChart(baseFile.Content)
Expand Down Expand Up @@ -351,19 +350,6 @@ func ParseHelmChart(content []byte) (*kotsv1beta1.HelmChart, error) {
return nil, errors.Errorf("not a HelmChart GVK: %s", gvk.String())
}

func isHelmChartKind(content []byte) bool {
gvk := OverlySimpleGVK{}

if err := stdyaml.Unmarshal(content, &gvk); err != nil {
return false
}

if gvk.APIVersion == "kots.io/v1beta1" && gvk.Kind == "HelmChart" {
return true
}
return false
}

func tryGetConfigFromFileContent(content []byte, log *logger.CLILogger) *kotsv1beta1.Config {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(content, nil, nil)
Expand Down
29 changes: 28 additions & 1 deletion pkg/kotsutil/kots.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/replicatedhq/troubleshoot/pkg/docrewrite"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"gopkg.in/yaml.v2"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
Expand All @@ -59,6 +60,17 @@ var (
}
)

type OverlySimpleGVK struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata OverlySimpleMetadata `yaml:"metadata"`
}

type OverlySimpleMetadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}

// KotsKinds are all of the special "client-side" kinds that are packaged in
// an application. These should be pointers because they are all optional.
// But a few are still expected in the code later, so we make them not pointers,
Expand Down Expand Up @@ -570,9 +582,13 @@ func LoadHelmChartsFromPath(fromDir string) ([]*kotsv1beta1.HelmChart, error) {
return errors.Wrap(err, "failed to read file")
}

if !IsHelmChartKind(contents) {
return nil
}

decoded, gvk, err := decode(contents, nil, nil)
if err != nil {
return nil
return errors.Wrap(err, "failed to decode")
}

if gvk.String() == "kots.io/v1beta1, Kind=HelmChart" {
Expand All @@ -590,6 +606,17 @@ func LoadHelmChartsFromPath(fromDir string) ([]*kotsv1beta1.HelmChart, error) {
return charts, nil
}

func IsHelmChartKind(content []byte) bool {
gvk := OverlySimpleGVK{}
if err := yaml.Unmarshal(content, &gvk); err != nil {
return false
}
if gvk.APIVersion == "kots.io/v1beta1" && gvk.Kind == "HelmChart" {
return true
}
return false
}

func LoadInstallationFromPath(installationFilePath string) (*kotsv1beta1.Installation, error) {
installationData, err := ioutil.ReadFile(installationFilePath)
if err != nil {
Expand Down

0 comments on commit db44f1a

Please sign in to comment.