Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix templating LicenseFieldValue on license sync #4468

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ kotsdata/
sbom/
cosign.key
pkg/tests/pull/cases/*/results
pkg/tests/renderdir/cases/*/results

melange.rsa
melange.rsa.pub
Expand Down
9 changes: 8 additions & 1 deletion pkg/handlers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/replicatedhq/kots/pkg/preflight"
registrytypes "github.com/replicatedhq/kots/pkg/registry/types"
"github.com/replicatedhq/kots/pkg/render"
rendertypes "github.com/replicatedhq/kots/pkg/render/types"
"github.com/replicatedhq/kots/pkg/replicatedapp"
"github.com/replicatedhq/kots/pkg/store"
storetypes "github.com/replicatedhq/kots/pkg/store/types"
Expand Down Expand Up @@ -893,7 +894,13 @@ func updateAppConfig(updateApp *apptypes.App, sequence int64, configGroups []kot
renderSequence = nextAppSequence
}

err = render.RenderDir(archiveDir, app, downstreams, registrySettings, renderSequence)
err = render.RenderDir(rendertypes.RenderDirOptions{
ArchiveDir: archiveDir,
App: app,
Downstreams: downstreams,
RegistrySettings: registrySettings,
Sequence: renderSequence,
})
if err != nil {
cause := errors.Cause(err)
if _, ok := cause.(util.ActionableError); ok {
Expand Down
9 changes: 8 additions & 1 deletion pkg/handlers/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/replicatedhq/kots/pkg/preflight"
"github.com/replicatedhq/kots/pkg/rbac"
"github.com/replicatedhq/kots/pkg/render"
rendertypes "github.com/replicatedhq/kots/pkg/render/types"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
"github.com/replicatedhq/kots/pkg/version"
Expand Down Expand Up @@ -455,7 +456,13 @@ func (h *Handler) ConfigureAppIdentityService(w http.ResponseWriter, r *http.Req
return
}

err = render.RenderDir(archiveDir, a, downstreams, registrySettings, nextAppSequence)
err = render.RenderDir(rendertypes.RenderDirOptions{
ArchiveDir: archiveDir,
App: a,
Downstreams: downstreams,
RegistrySettings: registrySettings,
Sequence: nextAppSequence,
})
if err != nil {
err = errors.Wrap(err, "failed to render archive directory")
logger.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (h *Handler) UpdateAppRegistry(w http.ResponseWriter, r *http.Request) {
appDir, err := registry.RewriteImages(
foundApp.ID, latestSequence, updateAppRegistryRequest.Hostname,
updateAppRegistryRequest.Username, registryPassword,
updateAppRegistryRequest.Namespace, skipImagePush, nil)
updateAppRegistryRequest.Namespace, skipImagePush)
if err != nil {
// log credential errors at info level
causeErr := errors.Cause(err)
Expand Down
9 changes: 8 additions & 1 deletion pkg/handlers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/preflight"
"github.com/replicatedhq/kots/pkg/render"
rendertypes "github.com/replicatedhq/kots/pkg/render/types"
"github.com/replicatedhq/kots/pkg/store"
storetypes "github.com/replicatedhq/kots/pkg/store/types"
"github.com/replicatedhq/kots/pkg/util"
Expand Down Expand Up @@ -158,7 +159,13 @@ func (h *Handler) UploadExistingApp(w http.ResponseWriter, r *http.Request) {
return
}

err = render.RenderDir(archiveDir, a, downstreams, registrySettings, nextAppSequence)
err = render.RenderDir(rendertypes.RenderDirOptions{
ArchiveDir: archiveDir,
App: a,
Downstreams: downstreams,
RegistrySettings: registrySettings,
Sequence: nextAppSequence,
})
if err != nil {
cause := errors.Cause(err)
if _, ok := cause.(util.ActionableError); ok {
Expand Down
10 changes: 9 additions & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,15 @@ func (o *Operator) renderKotsApplicationSpec(app *apptypes.App, sequence int64,
return nil, errors.Wrap(err, "failed to marshal kots app spec")
}

renderedKotsAppSpec, err := renderer.RenderFile(kotsKinds, registrySettings, app.Slug, sequence, app.IsAirgap, namespace, []byte(marshalledKotsAppSpec))
renderedKotsAppSpec, err := renderer.RenderFile(rendertypes.RenderFileOptions{
KotsKinds: kotsKinds,
RegistrySettings: registrySettings,
AppSlug: app.Slug,
Sequence: sequence,
IsAirgap: app.IsAirgap,
Namespace: namespace,
InputContent: []byte(marshalledKotsAppSpec),
})
if err != nil {
return nil, errors.Wrap(err, "failed to render preflights")
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/preflight/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
registrytypes "github.com/replicatedhq/kots/pkg/registry/types"
"github.com/replicatedhq/kots/pkg/render"
"github.com/replicatedhq/kots/pkg/render/helper"
rendertypes "github.com/replicatedhq/kots/pkg/render/types"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/store"
storetypes "github.com/replicatedhq/kots/pkg/store/types"
Expand Down Expand Up @@ -107,7 +108,15 @@ func Run(appID string, appSlug string, sequence int64, isAirgap bool, archiveDir
return errors.Wrap(err, "failed to marshal rendered preflight")
}

renderedPreflight, err := render.RenderFile(kotsKinds, registrySettings, appSlug, sequence, isAirgap, util.PodNamespace, []byte(renderedMarshalledPreflights))
renderedPreflight, err := render.RenderFile(rendertypes.RenderFileOptions{
KotsKinds: kotsKinds,
RegistrySettings: registrySettings,
AppSlug: appSlug,
Sequence: sequence,
IsAirgap: isAirgap,
Namespace: util.PodNamespace,
InputContent: []byte(renderedMarshalledPreflights),
})
if err != nil {
return errors.Wrap(err, "failed to render preflights")
}
Expand Down
33 changes: 19 additions & 14 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/replicatedhq/kots/pkg/rewrite"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -30,7 +29,7 @@ import (
// RewriteImages will use the app (a) and send the images to the registry specified. It will create patches for these
// and create a new version of the application
// the caller is responsible for deleting the appDir returned
func RewriteImages(appID string, sequence int64, hostname string, username string, password string, namespace string, isReadOnly bool, configValues *kotsv1beta1.ConfigValues) (appDir string, finalError error) {
func RewriteImages(appID string, sequence int64, hostname string, username string, password string, namespace string, isReadOnly bool) (appDir string, finalError error) {
if err := store.GetStore().SetTaskStatus("image-rewrite", "Updating registry settings", "running"); err != nil {
return "", errors.Wrap(err, "failed to set task status")
}
Expand Down Expand Up @@ -76,15 +75,6 @@ func RewriteImages(appID string, sequence int64, hostname string, username strin
return "", errors.Wrap(err, "failed to get app version archive")
}

kotsKinds, err := kotsutil.LoadKotsKinds(appDir)
if err != nil {
return "", errors.Wrap(err, "failed to load kotskinds from path")
}

if configValues == nil {
configValues = kotsKinds.ConfigValues
}

// get the downstream names only
downstreams, err := store.GetStore().ListDownstreamsForApp(appID)
if err != nil {
Expand Down Expand Up @@ -123,15 +113,30 @@ func RewriteImages(appID string, sequence int64, hostname string, username strin
return "", errors.Wrap(err, "failed to get next app sequence")
}

installation, err := kotsutil.LoadInstallationFromPath(filepath.Join(appDir, "upstream", "userdata", "installation.yaml"))
if err != nil {
return "", errors.Wrap(err, "failed to load installation from path")
}

license, err := kotsutil.LoadLicenseFromPath(filepath.Join(appDir, "upstream", "userdata", "license.yaml"))
if err != nil {
return "", errors.Wrap(err, "failed to load license from path")
}

configValues, err := kotsutil.LoadConfigValuesFromFile(filepath.Join(appDir, "upstream", "userdata", "config.yaml"))
if err != nil && !os.IsNotExist(errors.Cause(err)) {
return "", errors.Wrap(err, "failed to load config values from path")
}

options := rewrite.RewriteOptions{
RootDir: appDir,
UpstreamURI: fmt.Sprintf("replicated://%s", kotsKinds.License.Spec.AppSlug),
UpstreamURI: fmt.Sprintf("replicated://%s", license.Spec.AppSlug),
UpstreamPath: filepath.Join(appDir, "upstream"),
Installation: &kotsKinds.Installation,
Installation: installation,
Downstreams: downstreamNames,
CreateAppDir: false,
ExcludeKotsKinds: true,
License: kotsKinds.License,
License: license,
ConfigValues: configValues,
K8sNamespace: appNamespace,
ReportWriter: pipeWriter,
Expand Down
11 changes: 10 additions & 1 deletion pkg/render/helper/appfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/replicatedhq/kots/pkg/kotsutil"
registrytypes "github.com/replicatedhq/kots/pkg/registry/types"
"github.com/replicatedhq/kots/pkg/render"
rendertypes "github.com/replicatedhq/kots/pkg/render/types"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
)
Expand Down Expand Up @@ -33,5 +34,13 @@ func RenderAppFile(a types.AppType, overrideSequence *int64, inputContent []byte
registrySettings = s
}

return render.RenderFile(kotsKinds, registrySettings, a.GetSlug(), sequence, a.GetIsAirgap(), namespace, inputContent)
return render.RenderFile(rendertypes.RenderFileOptions{
KotsKinds: kotsKinds,
RegistrySettings: registrySettings,
AppSlug: a.GetSlug(),
Sequence: sequence,
IsAirgap: a.GetIsAirgap(),
Namespace: namespace,
InputContent: []byte(inputContent),
})
}
68 changes: 39 additions & 29 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"path/filepath"

"github.com/pkg/errors"
downstreamtypes "github.com/replicatedhq/kots/pkg/api/downstream/types"
apptypes "github.com/replicatedhq/kots/pkg/app/types"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/kotsutil"
registrytypes "github.com/replicatedhq/kots/pkg/registry/types"
types "github.com/replicatedhq/kots/pkg/render/types"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/rewrite"
"github.com/replicatedhq/kots/pkg/template"
Expand All @@ -23,28 +22,29 @@ type Renderer struct {

// RenderFile renders a single file
// this is useful for upstream/kotskinds files that are not rendered in the dir
func (r Renderer) RenderFile(kotsKinds *kotsutil.KotsKinds, registrySettings registrytypes.RegistrySettings, appSlug string, sequence int64, isAirgap bool, namespace string, inputContent []byte) ([]byte, error) {
return RenderFile(kotsKinds, registrySettings, appSlug, sequence, isAirgap, namespace, inputContent)
func (r Renderer) RenderFile(opts types.RenderFileOptions) ([]byte, error) {
return RenderFile(opts)
}

func RenderFile(kotsKinds *kotsutil.KotsKinds, registrySettings registrytypes.RegistrySettings, appSlug string, sequence int64, isAirgap bool, namespace string, inputContent []byte) ([]byte, error) {
fixedUpContent, err := kotsutil.FixUpYAML(inputContent)
func RenderFile(opts types.RenderFileOptions) ([]byte, error) {
fixedUpContent, err := kotsutil.FixUpYAML(opts.InputContent)
if err != nil {
return nil, errors.Wrap(err, "failed to fix up yaml")
}
opts.InputContent = fixedUpContent

return RenderContent(kotsKinds, registrySettings, appSlug, sequence, isAirgap, namespace, fixedUpContent)
return RenderContent(opts)
}

// RenderContent renders any string/content
// this is useful for rendering single values, like a status informer
func RenderContent(kotsKinds *kotsutil.KotsKinds, registrySettings registrytypes.RegistrySettings, appSlug string, sequence int64, isAirgap bool, namespace string, inputContent []byte) ([]byte, error) {
builder, err := NewBuilder(kotsKinds, registrySettings, appSlug, sequence, isAirgap, namespace)
func RenderContent(opts types.RenderFileOptions) ([]byte, error) {
builder, err := NewBuilder(opts.KotsKinds, opts.RegistrySettings, opts.AppSlug, opts.Sequence, opts.IsAirgap, opts.Namespace)
if err != nil {
return nil, errors.Wrap(err, "failed to create builder")
}

rendered, err := builder.RenderTemplate(string(inputContent), string(inputContent))
rendered, err := builder.RenderTemplate(string(opts.InputContent), string(opts.InputContent))
if err != nil {
return nil, errors.Wrap(err, "failed to render")
}
Expand Down Expand Up @@ -97,18 +97,28 @@ func NewBuilder(kotsKinds *kotsutil.KotsKinds, registrySettings registrytypes.Re

// RenderDir renders an app archive dir
// this is useful for when the license/config have updated, and template functions need to be evaluated again
func (r Renderer) RenderDir(archiveDir string, a *apptypes.App, downstreams []downstreamtypes.Downstream, registrySettings registrytypes.RegistrySettings, sequence int64) error {
return RenderDir(archiveDir, a, downstreams, registrySettings, sequence)
func (r Renderer) RenderDir(opts types.RenderDirOptions) error {
return RenderDir(opts)
}

func RenderDir(archiveDir string, a *apptypes.App, downstreams []downstreamtypes.Downstream, registrySettings registrytypes.RegistrySettings, sequence int64) error {
kotsKinds, err := kotsutil.LoadKotsKinds(archiveDir)
func RenderDir(opts types.RenderDirOptions) error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the beginning of this function where loading the installation, license, and config values from the upstream directory rather than the kotsKinds directory when creating a new version is what addresses the regression.

installation, err := kotsutil.LoadInstallationFromPath(filepath.Join(opts.ArchiveDir, "upstream", "userdata", "installation.yaml"))
if err != nil {
return errors.Wrap(err, "failed to load kotskinds from path")
return errors.Wrap(err, "failed to load installation from path")
}

license, err := kotsutil.LoadLicenseFromPath(filepath.Join(opts.ArchiveDir, "upstream", "userdata", "license.yaml"))
if err != nil {
return errors.Wrap(err, "failed to load license from path")
}

configValues, err := kotsutil.LoadConfigValuesFromFile(filepath.Join(opts.ArchiveDir, "upstream", "userdata", "config.yaml"))
if err != nil && !os.IsNotExist(errors.Cause(err)) {
return errors.Wrap(err, "failed to load config values from path")
}

downstreamNames := []string{}
for _, d := range downstreams {
for _, d := range opts.Downstreams {
downstreamNames = append(downstreamNames, d.Name)
}

Expand All @@ -118,25 +128,25 @@ func RenderDir(archiveDir string, a *apptypes.App, downstreams []downstreamtypes
}

reOptions := rewrite.RewriteOptions{
RootDir: archiveDir,
UpstreamURI: fmt.Sprintf("replicated://%s", kotsKinds.License.Spec.AppSlug),
UpstreamPath: filepath.Join(archiveDir, "upstream"),
Installation: &kotsKinds.Installation,
RootDir: opts.ArchiveDir,
UpstreamURI: fmt.Sprintf("replicated://%s", license.Spec.AppSlug),
UpstreamPath: filepath.Join(opts.ArchiveDir, "upstream"),
Installation: installation,
Downstreams: downstreamNames,
Silent: true,
CreateAppDir: false,
ExcludeKotsKinds: true,
License: kotsKinds.License,
ConfigValues: kotsKinds.ConfigValues,
License: license,
ConfigValues: configValues,
K8sNamespace: appNamespace,
CopyImages: false,
IsAirgap: a.IsAirgap,
AppID: a.ID,
AppSlug: a.Slug,
IsGitOps: a.IsGitOps,
AppSequence: sequence,
ReportingInfo: reporting.GetReportingInfo(a.ID),
RegistrySettings: registrySettings,
IsAirgap: opts.App.IsAirgap,
AppID: opts.App.ID,
AppSlug: opts.App.Slug,
IsGitOps: opts.App.IsGitOps,
AppSequence: opts.Sequence,
ReportingInfo: reporting.GetReportingInfo(opts.App.ID),
RegistrySettings: opts.RegistrySettings,

// TODO: pass in as arguments if this is ever called from CLI
HTTPProxyEnvValue: os.Getenv("HTTP_PROXY"),
Expand Down
22 changes: 20 additions & 2 deletions pkg/render/types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@ import (
registrytypes "github.com/replicatedhq/kots/pkg/registry/types"
)

type RenderFileOptions struct {
KotsKinds *kotsutil.KotsKinds
RegistrySettings registrytypes.RegistrySettings
AppSlug string
Sequence int64
IsAirgap bool
Namespace string
InputContent []byte
}

type RenderDirOptions struct {
ArchiveDir string
App *apptypes.App
Downstreams []downstreamtypes.Downstream
RegistrySettings registrytypes.RegistrySettings
Sequence int64
}

type Renderer interface {
RenderFile(kotsKinds *kotsutil.KotsKinds, registrySettings registrytypes.RegistrySettings, appSlug string, sequence int64, isAirgap bool, namespace string, inputContent []byte) ([]byte, error)
RenderDir(archiveDir string, a *apptypes.App, downstreams []downstreamtypes.Downstream, registrySettings registrytypes.RegistrySettings, sequence int64) error
RenderFile(opts RenderFileOptions) ([]byte, error)
RenderDir(opts RenderDirOptions) error
}
8 changes: 7 additions & 1 deletion pkg/store/kotsstore/license_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ func (s *KOTSStore) createNewVersionForLicenseChangeStatements(appID string, bas
return nil, int64(0), errors.Wrap(err, "failed to get next app sequence")
}

if err := renderer.RenderDir(archiveDir, app, downstreams, registrySettings, nextAppSequence); err != nil {
if err := renderer.RenderDir(rendertypes.RenderDirOptions{
ArchiveDir: archiveDir,
App: app,
Downstreams: downstreams,
RegistrySettings: registrySettings,
Sequence: nextAppSequence,
}); err != nil {
return nil, int64(0), errors.Wrap(err, "failed to render new version")
}

Expand Down
Loading
Loading