Skip to content

Commit

Permalink
Respect proxyPublicImages flag when rendering app spec
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin committed Aug 26, 2021
1 parent d5b1c9b commit 40ffb94
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 49 deletions.
73 changes: 52 additions & 21 deletions pkg/base/replicated.go
Expand Up @@ -14,17 +14,30 @@ import (

"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
kotsscheme "github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme"
kotsconfig "github.com/replicatedhq/kots/pkg/config"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/template"
upstreamtypes "github.com/replicatedhq/kots/pkg/upstream/types"
"github.com/replicatedhq/kots/pkg/util"
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"
"sigs.k8s.io/yaml"
)

func init() {
kotsscheme.AddToScheme(scheme.Scheme)
troubleshootscheme.AddToScheme(scheme.Scheme)
velerov1.AddToScheme(scheme.Scheme)
applicationv1beta1.AddToScheme(scheme.Scheme)
}

type Document struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Expand All @@ -41,12 +54,12 @@ func renderReplicated(u *upstreamtypes.Upstream, renderOptions *RenderOptions) (
return nil, nil, errors.Wrap(err, "failed to create new config context template builder")
}

config, _, idConfig, license, err := findConfigAndLicense(u, renderOptions.Log)
kotsKinds, err := getKotsKinds(u, renderOptions.Log)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to find config file")
}

renderedConfig, err := kotsconfig.TemplateConfigObjects(config, itemValues, license, template.LocalRegistry{}, nil, idConfig, util.PodNamespace)
renderedConfig, err := kotsconfig.TemplateConfigObjects(kotsKinds.Config, itemValues, kotsKinds.License, &kotsKinds.KotsApplication, template.LocalRegistry{}, nil, kotsKinds.IdentityConfig, util.PodNamespace)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to template config objects")
}
Expand Down Expand Up @@ -348,11 +361,8 @@ func tryGetConfigFromFileContent(content []byte, log *logger.CLILogger) *kotsv1b
return nil
}

func findConfigAndLicense(u *upstreamtypes.Upstream, log *logger.CLILogger) (*kotsv1beta1.Config, *kotsv1beta1.ConfigValues, *kotsv1beta1.IdentityConfig, *kotsv1beta1.License, error) {
var config *kotsv1beta1.Config
var values *kotsv1beta1.ConfigValues
var identityConfig *kotsv1beta1.IdentityConfig
var license *kotsv1beta1.License
func getKotsKinds(u *upstreamtypes.Upstream, log *logger.CLILogger) (*kotsutil.KotsKinds, error) {
kotsKinds := &kotsutil.KotsKinds{}

for _, file := range u.Files {
document := &Document{}
Expand All @@ -361,27 +371,48 @@ func findConfigAndLicense(u *upstreamtypes.Upstream, log *logger.CLILogger) (*ko
}

decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(file.Content, nil, nil)
decoded, gvk, err := decode(file.Content, nil, nil)
if err != nil {
if document.APIVersion == "kots.io/v1beta1" && (document.Kind == "Config" || document.Kind == "License") {
errMessage := fmt.Sprintf("Failed to decode %s", file.Path)
return nil, nil, nil, nil, errors.Wrap(err, errMessage)
return nil, errors.Wrap(err, errMessage)
}
continue
}

if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "Config" {
config = obj.(*kotsv1beta1.Config)
} else if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "ConfigValues" {
values = obj.(*kotsv1beta1.ConfigValues)
} else if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "IdentityConfig" {
identityConfig = obj.(*kotsv1beta1.IdentityConfig)
} else if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "License" {
license = obj.(*kotsv1beta1.License)
}
}

return config, values, identityConfig, license, nil
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 kotsKinds, nil
}

// findHelmChartArchiveInRelease iterates through all files in the release (upstreamFiles), looking for a helm chart archive
Expand Down
15 changes: 8 additions & 7 deletions pkg/base/templates.go
Expand Up @@ -9,15 +9,15 @@ import (
)

func NewConfigContextTemplateBuilder(u *upstreamtypes.Upstream, renderOptions *RenderOptions) (*template.Builder, map[string]template.ItemValue, error) {
config, configValues, identityConfig, license, err := findConfigAndLicense(u, renderOptions.Log)
kotsKinds, err := getKotsKinds(u, renderOptions.Log)
if err != nil {
return nil, nil, err
}

var templateContext map[string]template.ItemValue
if configValues != nil {
if kotsKinds.ConfigValues != nil {
ctx := map[string]template.ItemValue{}
for k, v := range configValues.Spec.Values {
for k, v := range kotsKinds.ConfigValues.Spec.Values {
ctx[k] = template.ItemValue{
Value: v.Value,
Default: v.Default,
Expand All @@ -40,8 +40,8 @@ func NewConfigContextTemplateBuilder(u *upstreamtypes.Upstream, renderOptions *R
}

configGroups := []kotsv1beta1.ConfigGroup{}
if config != nil {
configGroups = config.Spec.Groups
if kotsKinds.Config != nil {
configGroups = kotsKinds.Config.Spec.Groups
}

localRegistry := template.LocalRegistry{
Expand Down Expand Up @@ -70,10 +70,11 @@ func NewConfigContextTemplateBuilder(u *upstreamtypes.Upstream, renderOptions *R
ExistingValues: templateContext,
LocalRegistry: localRegistry,
Cipher: cipher,
License: license,
License: kotsKinds.License,
Application: &kotsKinds.KotsApplication,
VersionInfo: &versionInfo,
ApplicationInfo: &appInfo,
IdentityConfig: identityConfig,
IdentityConfig: kotsKinds.IdentityConfig,
Namespace: renderOptions.Namespace,
}
builder, itemValues, err := template.NewBuilder(builderOptions)
Expand Down
27 changes: 20 additions & 7 deletions pkg/config/config.go
Expand Up @@ -17,12 +17,12 @@ import (
"k8s.io/client-go/kubernetes/scheme"
)

func TemplateConfig(log *logger.CLILogger, configSpecData string, configValuesData string, licenseData string, identityConfigData string, localRegistry template.LocalRegistry, namespace string) (string, error) {
return templateConfig(log, configSpecData, configValuesData, licenseData, identityConfigData, localRegistry, namespace, MarshalConfig)
func TemplateConfig(log *logger.CLILogger, configSpecData string, configValuesData string, licenseData string, appData string, identityConfigData string, localRegistry template.LocalRegistry, namespace string) (string, error) {
return templateConfig(log, configSpecData, configValuesData, licenseData, appData, identityConfigData, localRegistry, namespace, MarshalConfig)
}

func TemplateConfigObjects(configSpec *kotsv1beta1.Config, configValues map[string]template.ItemValue, license *kotsv1beta1.License, localRegistry template.LocalRegistry, versionInfo *template.VersionInfo, identityconfig *kotsv1beta1.IdentityConfig, namespace string) (*kotsv1beta1.Config, error) {
templatedString, err := templateConfigObjects(configSpec, configValues, license, localRegistry, versionInfo, identityconfig, namespace, MarshalConfig)
func TemplateConfigObjects(configSpec *kotsv1beta1.Config, configValues map[string]template.ItemValue, license *kotsv1beta1.License, app *kotsv1beta1.Application, localRegistry template.LocalRegistry, versionInfo *template.VersionInfo, identityconfig *kotsv1beta1.IdentityConfig, namespace string) (*kotsv1beta1.Config, error) {
templatedString, err := templateConfigObjects(configSpec, configValues, license, app, localRegistry, versionInfo, identityconfig, namespace, MarshalConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to template config")
}
Expand All @@ -43,7 +43,7 @@ func TemplateConfigObjects(configSpec *kotsv1beta1.Config, configValues map[stri
return config, nil
}

func templateConfigObjects(configSpec *kotsv1beta1.Config, configValues map[string]template.ItemValue, license *kotsv1beta1.License, localRegistry template.LocalRegistry, versionInfo *template.VersionInfo, identityconfig *kotsv1beta1.IdentityConfig, namespace string, marshalFunc func(config *kotsv1beta1.Config) (string, error)) (string, error) {
func templateConfigObjects(configSpec *kotsv1beta1.Config, configValues map[string]template.ItemValue, license *kotsv1beta1.License, app *kotsv1beta1.Application, localRegistry template.LocalRegistry, versionInfo *template.VersionInfo, identityconfig *kotsv1beta1.IdentityConfig, namespace string, marshalFunc func(config *kotsv1beta1.Config) (string, error)) (string, error) {
if configSpec == nil {
return "", nil
}
Expand All @@ -54,6 +54,7 @@ func templateConfigObjects(configSpec *kotsv1beta1.Config, configValues map[stri
LocalRegistry: localRegistry,
Cipher: nil,
License: license,
Application: app,
VersionInfo: versionInfo,
IdentityConfig: identityconfig,
Namespace: namespace,
Expand All @@ -78,7 +79,7 @@ func templateConfigObjects(configSpec *kotsv1beta1.Config, configValues map[stri
return rendered, nil
}

func templateConfig(log *logger.CLILogger, configSpecData string, configValuesData string, licenseData string, identityConfigData string, localRegistry template.LocalRegistry, namespace string, marshalFunc func(config *kotsv1beta1.Config) (string, error)) (string, error) {
func templateConfig(log *logger.CLILogger, configSpecData string, configValuesData string, licenseData string, appData string, identityConfigData string, localRegistry template.LocalRegistry, namespace string, marshalFunc func(config *kotsv1beta1.Config) (string, error)) (string, error) {
// This function will
// 1. unmarshal config
// 2. replace all item values with values that already exist
Expand All @@ -96,6 +97,18 @@ func templateConfig(log *logger.CLILogger, configSpecData string, configValuesDa
}
license := obj.(*kotsv1beta1.License)

var app *kotsv1beta1.Application
if appData != "" {
obj, gvk, err = decode([]byte(appData), nil, nil)
if err != nil {
return "", errors.Wrap(err, "failed to decode applicationappData := ` data")
}
if gvk.Group != "kots.io" || gvk.Version != "v1beta1" || gvk.Kind != "Application" {
return "", errors.Errorf("expected Application, but found %s/%s/%s", gvk.Group, gvk.Version, gvk.Kind)
}
app = obj.(*kotsv1beta1.Application)
}

obj, gvk, err = decode([]byte(configSpecData), nil, nil) // TODO fix decode of boolstrings
if err != nil {
return "", errors.Wrap(err, "failed to decode config data")
Expand Down Expand Up @@ -124,7 +137,7 @@ func templateConfig(log *logger.CLILogger, configSpecData string, configValuesDa
identityConfig = obj.(*kotsv1beta1.IdentityConfig)
}

return templateConfigObjects(config, templateContext, license, localRegistry, &template.VersionInfo{}, identityConfig, namespace, marshalFunc)
return templateConfigObjects(config, templateContext, license, app, localRegistry, &template.VersionInfo{}, identityConfig, namespace, marshalFunc)
}

func ApplyValuesToConfig(config *kotsv1beta1.Config, values map[string]template.ItemValue) *kotsv1beta1.Config {
Expand Down
22 changes: 20 additions & 2 deletions pkg/config/config_test.go
Expand Up @@ -61,10 +61,19 @@ spec:
value: "123asd"
signature: IA==`

appSpec := `
apiVersion: kots.io/v1beta1
kind: Application
metadata:
name: local
spec:
title: My Application`

tests := []struct {
name string
configSpecData string
configValuesData string
useAppSpec bool
want string
expectOldFail bool
}{
Expand Down Expand Up @@ -96,6 +105,7 @@ spec:
value: "xyz789"
status: {}
`,
useAppSpec: true,
want: `apiVersion: kots.io/v1beta1
kind: Config
metadata:
Expand Down Expand Up @@ -147,6 +157,7 @@ spec:
values: {}
status: {}
`,
useAppSpec: false,
want: `apiVersion: kots.io/v1beta1
kind: Config
metadata:
Expand Down Expand Up @@ -213,6 +224,7 @@ spec:
value: "xyz789"
status: {}
`,
useAppSpec: false,
want: `apiVersion: kots.io/v1beta1
kind: Config
metadata:
Expand Down Expand Up @@ -286,6 +298,7 @@ spec:
repeatableItem: secretName
status: {}
`,
useAppSpec: true,
want: `apiVersion: kots.io/v1beta1
kind: Config
metadata:
Expand Down Expand Up @@ -331,8 +344,13 @@ spec:
wantObj, _, err := decode([]byte(tt.want), nil, nil)
req.NoError(err)

appData := ""
if tt.useAppSpec {
appData = appSpec
}

localRegistry := template.LocalRegistry{}
got, err := templateConfig(log, tt.configSpecData, tt.configValuesData, licenseData, "", localRegistry, "", MarshalConfig)
got, err := templateConfig(log, tt.configSpecData, tt.configValuesData, licenseData, appData, "", localRegistry, "", MarshalConfig)
req.NoError(err)

gotObj, _, err := decode([]byte(got), nil, nil)
Expand All @@ -341,7 +359,7 @@ spec:
req.Equal(wantObj, gotObj)

// compare with oldMarshalConfig results
got, err = templateConfig(log, tt.configSpecData, tt.configValuesData, licenseData, "", localRegistry, "", oldMarshalConfig)
got, err = templateConfig(log, tt.configSpecData, tt.configValuesData, licenseData, appData, "", localRegistry, "", oldMarshalConfig)
if !tt.expectOldFail {
req.NoError(err)

Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/config.go
Expand Up @@ -241,7 +241,7 @@ func (h *Handler) LiveAppConfig(w http.ResponseWriter, r *http.Request) {
}

versionInfo := template.VersionInfoFromInstallation(liveAppConfigRequest.Sequence+1, foundApp.IsAirgap, kotsKinds.Installation.Spec) // sequence +1 because the sequence will be incremented on save (and we want the preview to be accurate)
renderedConfig, err := kotsconfig.TemplateConfigObjects(kotsKinds.Config, configValues, appLicense, localRegistry, &versionInfo, kotsKinds.IdentityConfig, util.PodNamespace)
renderedConfig, err := kotsconfig.TemplateConfigObjects(kotsKinds.Config, configValues, appLicense, &kotsKinds.KotsApplication, localRegistry, &versionInfo, kotsKinds.IdentityConfig, util.PodNamespace)
if err != nil {
logger.Error(err)
liveAppConfigResponse.Error = "failed to render templates"
Expand Down Expand Up @@ -341,7 +341,7 @@ func (h *Handler) CurrentAppConfig(w http.ResponseWriter, r *http.Request) {
}

versionInfo := template.VersionInfoFromInstallation(int64(sequence)+1, foundApp.IsAirgap, kotsKinds.Installation.Spec) // sequence +1 because the sequence will be incremented on save (and we want the preview to be accurate)
renderedConfig, err := kotsconfig.TemplateConfigObjects(kotsKinds.Config, configValues, appLicense, localRegistry, &versionInfo, kotsKinds.IdentityConfig, util.PodNamespace)
renderedConfig, err := kotsconfig.TemplateConfigObjects(kotsKinds.Config, configValues, appLicense, &kotsKinds.KotsApplication, localRegistry, &versionInfo, kotsKinds.IdentityConfig, util.PodNamespace)
if err != nil {
logger.Error(err)
currentAppConfigResponse.Error = "failed to render templates"
Expand Down Expand Up @@ -774,7 +774,7 @@ func (h *Handler) SetAppConfigValues(w http.ResponseWriter, r *http.Request) {
}

versionInfo := template.VersionInfoFromInstallation(foundApp.CurrentSequence+1, foundApp.IsAirgap, kotsKinds.Installation.Spec) // sequence +1 because the sequence will be incremented on save (and we want the preview to be accurate)
renderedConfig, err := kotsconfig.TemplateConfigObjects(newConfig, configValueMap, kotsKinds.License, localRegistry, &versionInfo, kotsKinds.IdentityConfig, util.PodNamespace)
renderedConfig, err := kotsconfig.TemplateConfigObjects(newConfig, configValueMap, kotsKinds.License, &kotsKinds.KotsApplication, localRegistry, &versionInfo, kotsKinds.IdentityConfig, util.PodNamespace)
if err != nil {
setAppConfigValuesResponse.Error = "failed to render templates"
logger.Error(errors.Wrap(err, setAppConfigValuesResponse.Error))
Expand Down
7 changes: 6 additions & 1 deletion pkg/kotsadmconfig/config.go
Expand Up @@ -64,6 +64,11 @@ func NeedsConfiguration(kotsKinds *kotsutil.KotsKinds, registrySettings registry
return false, errors.Wrap(err, "failed to marshal license spec")
}

appSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "Application")
if err != nil {
return false, errors.Wrap(err, "failed to marshal license spec")
}

identityConfigSpec, err := kotsKinds.Marshal("kots.io", "v1beta1", "IdentityConfig")
if err != nil {
return false, errors.Wrap(err, "failed to marshal identityconfig spec")
Expand All @@ -77,7 +82,7 @@ func NeedsConfiguration(kotsKinds *kotsutil.KotsKinds, registrySettings registry
ReadOnly: registrySettings.IsReadOnly,
}

rendered, err := kotsconfig.TemplateConfig(logger.NewCLILogger(), configSpec, configValuesSpec, licenseSpec, identityConfigSpec, localRegistry, util.PodNamespace)
rendered, err := kotsconfig.TemplateConfig(logger.NewCLILogger(), configSpec, configValuesSpec, licenseSpec, appSpec, identityConfigSpec, localRegistry, util.PodNamespace)
if err != nil {
return false, errors.Wrap(err, "failed to template config")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/render/render.go
Expand Up @@ -93,6 +93,7 @@ func NewBuilder(kotsKinds *kotsutil.KotsKinds, registrySettings registrytypes.Re
LocalRegistry: localRegistry,
Cipher: appCipher,
License: kotsKinds.License,
Application: &kotsKinds.KotsApplication,
ApplicationInfo: &appInfo,
VersionInfo: &versionInfo,
IdentityConfig: kotsKinds.IdentityConfig,
Expand Down
4 changes: 3 additions & 1 deletion pkg/template/builder.go
Expand Up @@ -28,6 +28,7 @@ type BuilderOptions struct {
LocalRegistry LocalRegistry
Cipher *crypto.AESCipher
License *kotsv1beta1.License
Application *kotsv1beta1.Application
ApplicationInfo *ApplicationInfo
VersionInfo *VersionInfo
IdentityConfig *kotsv1beta1.IdentityConfig
Expand All @@ -51,7 +52,8 @@ func NewBuilder(opts BuilderOptions) (Builder, map[string]ItemValue, error) {
}
}

configCtx, err := b.newConfigContext(opts.ConfigGroups, opts.ExistingValues, opts.LocalRegistry, opts.Cipher, opts.License, opts.VersionInfo, dockerHubRegistry)
configCtx, err := b.newConfigContext(opts.ConfigGroups, opts.ExistingValues, opts.LocalRegistry, opts.Cipher,
opts.License, opts.Application, opts.VersionInfo, dockerHubRegistry)
if err != nil {
return Builder{}, nil, errors.Wrap(err, "create config context")
}
Expand Down

0 comments on commit 40ffb94

Please sign in to comment.