Skip to content

Commit

Permalink
Merge branch 'main' into refactor/break-apart-insecure-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinAbro321 authored Sep 12, 2024
2 parents 77536f5 + e72a273 commit 868c7c0
Show file tree
Hide file tree
Showing 29 changed files with 318 additions and 144 deletions.
4 changes: 3 additions & 1 deletion src/cmd/common/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ func printViperConfigUsed() {
message.WarnErrf(vConfigError, lang.CmdViperErrLoadingConfigFile, vConfigError.Error())
return
}
message.Notef(lang.CmdViperInfoUsingConfigFile, v.ConfigFileUsed())
if cfgFile := v.ConfigFileUsed(); cfgFile != "" {
message.Notef(lang.CmdViperInfoUsingConfigFile, cfgFile)
}
}

func setDefaults() {
Expand Down
7 changes: 5 additions & 2 deletions src/cmd/tools/helm/load_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,14 @@ func loadFile(path string) (*pluginCommand, error) {
cmds := new(pluginCommand)
b, err := os.ReadFile(path)
if err != nil {
return cmds, fmt.Errorf("file (%s) not provided by plugin. No plugin auto-completion possible", path)
return nil, fmt.Errorf("file (%s) not provided by plugin. No plugin auto-completion possible", path)
}

err = yaml.Unmarshal(b, cmds)
return cmds, err
if err != nil {
return nil, err
}
return cmds, nil
}

// pluginDynamicComp call the plugin.complete script of the plugin (if available)
Expand Down
5 changes: 2 additions & 3 deletions src/internal/agent/hooks/argocd-application.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewApplicationMutationHook(ctx context.Context, cluster *cluster.Cluster) o
}

// mutateApplication mutates the git repository url to point to the repository URL defined in the ZarfState.
func mutateApplication(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (result *operations.Result, err error) {
func mutateApplication(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (*operations.Result, error) {
state, err := cluster.LoadZarfState(ctx)
if err != nil {
return nil, err
Expand All @@ -72,8 +72,7 @@ func mutateApplication(ctx context.Context, r *v1.AdmissionRequest, cluster *clu
return nil, fmt.Errorf(lang.ErrUnmarshal, err)
}

patches := []operations.PatchOperation{}

patches := make([]operations.PatchOperation, 0)
if app.Spec.Source != nil {
patchedURL, err := getPatchedRepoURL(app.Spec.Source.RepoURL, state.GitServer, r)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/agent/hooks/argocd-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewRepositorySecretMutationHook(ctx context.Context, cluster *cluster.Clust
}

// mutateRepositorySecret mutates the git URL in the ArgoCD repository secret to point to the repository URL defined in the ZarfState.
func mutateRepositorySecret(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (result *operations.Result, err error) {
func mutateRepositorySecret(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (*operations.Result, error) {
isCreate := r.Operation == v1.Create
isUpdate := r.Operation == v1.Update
var isPatched bool
Expand Down
2 changes: 1 addition & 1 deletion src/internal/agent/hooks/flux-gitrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewGitRepositoryMutationHook(ctx context.Context, cluster *cluster.Cluster)
}

// mutateGitRepoCreate mutates the git repository url to point to the repository URL defined in the ZarfState.
func mutateGitRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (result *operations.Result, err error) {
func mutateGitRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster.Cluster) (*operations.Result, error) {
var (
patches []operations.PatchOperation
isPatched bool
Expand Down
2 changes: 2 additions & 0 deletions src/pkg/cluster/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

// UpdateGiteaPVC updates the existing Gitea persistent volume claim and tells Gitea whether to create or not.
// TODO(mkcp): We return both string true/false and errors here so our callers get a string. This should be returning an
// empty val if we error, but we'll have to refactor upstream beforehand.
func (c *Cluster) UpdateGiteaPVC(ctx context.Context, pvcName string, shouldRollBack bool) (string, error) {
if shouldRollBack {
pvc, err := c.Clientset.CoreV1().PersistentVolumeClaims(ZarfNamespaceName).Get(ctx, pvcName, metav1.GetOptions{})
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/cluster/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,14 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO
}

// LoadZarfState returns the current zarf/zarf-state secret data or an empty ZarfState.
func (c *Cluster) LoadZarfState(ctx context.Context) (state *types.ZarfState, err error) {
func (c *Cluster) LoadZarfState(ctx context.Context) (*types.ZarfState, error) {
stateErr := errors.New("failed to load the Zarf State from the cluster, has Zarf been initiated?")
secret, err := c.Clientset.CoreV1().Secrets(ZarfNamespaceName).Get(ctx, ZarfStateSecretName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("%w: %w", stateErr, err)
}

state := &types.ZarfState{}
err = json.Unmarshal(secret.Data[ZarfStateDataKey], &state)
if err != nil {
return nil, fmt.Errorf("%w: %w", stateErr, err)
Expand Down
7 changes: 4 additions & 3 deletions src/pkg/cluster/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func (c *Cluster) ListConnections(ctx context.Context) (types.ConnectStrings, er

// NewTargetTunnelInfo returns a new TunnelInfo object for the specified target.
func (c *Cluster) NewTargetTunnelInfo(ctx context.Context, target string) (TunnelInfo, error) {
var err error
zt := TunnelInfo{
Namespace: ZarfNamespaceName,
ResourceType: SvcResource,
Expand All @@ -102,9 +101,11 @@ func (c *Cluster) NewTargetTunnelInfo(ctx context.Context, target string) (Tunne
zt.RemotePort = ZarfInjectorPort
default:
if target != "" {
if zt, err = c.checkForZarfConnectLabel(ctx, target); err != nil {
ztNew, err := c.checkForZarfConnectLabel(ctx, target)
if err != nil {
return TunnelInfo{}, fmt.Errorf("problem looking for a zarf connect label in the cluster: %s", err.Error())
}
zt = ztNew
}
if zt.ResourceName == "" {
return TunnelInfo{}, fmt.Errorf("missing resource name")
Expand All @@ -113,7 +114,7 @@ func (c *Cluster) NewTargetTunnelInfo(ctx context.Context, target string) (Tunne
return TunnelInfo{}, fmt.Errorf("missing remote port")
}
}
return zt, err
return zt, nil
}

// Connect will establish a tunnel to the specified target.
Expand Down
22 changes: 15 additions & 7 deletions src/pkg/cluster/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ func (c *Cluster) GetDeployedZarfPackages(ctx context.Context) ([]types.Deployed
deployedPackages = append(deployedPackages, deployedPackage)
}

return deployedPackages, errors.Join(errs...)
err = errors.Join(errs...)
if err != nil {
return nil, err
}
return deployedPackages, nil
}

// GetDeployedPackage gets the metadata information about the package name provided (if it exists in the cluster).
Expand Down Expand Up @@ -174,7 +178,7 @@ func (c *Cluster) RecordPackageDeploymentAndWait(ctx context.Context, pkg v1alph
}

// RecordPackageDeployment saves metadata about a package that has been deployed to the cluster.
func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.ZarfPackage, components []types.DeployedComponent, generation int) (deployedPackage *types.DeployedPackage, err error) {
func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.ZarfPackage, components []types.DeployedComponent, generation int) (*types.DeployedPackage, error) {
packageName := pkg.Metadata.Name

// Attempt to load information about webhooks for the package
Expand All @@ -187,7 +191,7 @@ func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.Zarf
componentWebhooks = existingPackageSecret.ComponentWebhooks
}

// TODO: This is done for backwards compartibility and could be removed in the future.
// TODO: This is done for backwards compatibility and could be removed in the future.
connectStrings := types.ConnectStrings{}
for _, comp := range components {
for _, chart := range comp.InstalledCharts {
Expand All @@ -197,7 +201,7 @@ func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.Zarf
}
}

deployedPackage = &types.DeployedPackage{
deployedPackage := &types.DeployedPackage{
Name: packageName,
CLIVersion: config.CLIVersion,
Data: pkg,
Expand Down Expand Up @@ -285,12 +289,13 @@ func (c *Cluster) DisableRegHPAScaleDown(ctx context.Context) error {
}

// GetInstalledChartsForComponent returns any installed Helm Charts for the provided package component.
func (c *Cluster) GetInstalledChartsForComponent(ctx context.Context, packageName string, component v1alpha1.ZarfComponent) (installedCharts []types.InstalledChart, err error) {
func (c *Cluster) GetInstalledChartsForComponent(ctx context.Context, packageName string, component v1alpha1.ZarfComponent) ([]types.InstalledChart, error) {
deployedPackage, err := c.GetDeployedPackage(ctx, packageName)
if err != nil {
return installedCharts, err
return nil, err
}

installedCharts := make([]types.InstalledChart, 0)
for _, deployedComponent := range deployedPackage.DeployedComponents {
if deployedComponent.Name == component.Name {
installedCharts = append(installedCharts, deployedComponent.InstalledCharts...)
Expand Down Expand Up @@ -324,7 +329,10 @@ func (c *Cluster) UpdateInternalArtifactServerToken(ctx context.Context, oldGitS
}
return nil
})
return newToken, err
if err != nil {
return "", err
}
return newToken, nil
}

// UpdateInternalGitServerSecret updates the internal gitea server secrets with the new git server info
Expand Down
9 changes: 7 additions & 2 deletions src/pkg/interactive/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// SelectOptionalComponent prompts to confirm optional components
func SelectOptionalComponent(component v1alpha1.ZarfComponent) (confirm bool, err error) {
func SelectOptionalComponent(component v1alpha1.ZarfComponent) (bool, error) {
message.HorizontalRule()

displayComponent := component
Expand All @@ -30,7 +30,12 @@ func SelectOptionalComponent(component v1alpha1.ZarfComponent) (confirm bool, er
Default: component.Default,
}

return confirm, survey.AskOne(prompt, &confirm)
var confirm bool
err := survey.AskOne(prompt, &confirm)
if err != nil {
return false, err
}
return confirm, nil
}

// SelectChoiceGroup prompts to select component groups
Expand Down
15 changes: 12 additions & 3 deletions src/pkg/interactive/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ func PromptSigPassword() ([]byte, error) {
prompt := &survey.Password{
Message: "Private key password (empty for no password): ",
}
return []byte(password), survey.AskOne(prompt, &password)
err := survey.AskOne(prompt, &password)
if err != nil {
return []byte{}, err
}
return []byte(password), nil
}

// PromptVariable prompts the user for a value for a variable
func PromptVariable(variable v1alpha1.InteractiveVariable) (value string, err error) {
func PromptVariable(variable v1alpha1.InteractiveVariable) (string, error) {
if variable.Description != "" {
message.Question(variable.Description)
}
Expand All @@ -33,5 +37,10 @@ func PromptVariable(variable v1alpha1.InteractiveVariable) (value string, err er
Default: variable.Default,
}

return value, survey.AskOne(prompt, &value)
var value string
err := survey.AskOne(prompt, &value)
if err != nil {
return "", err
}
return value, nil
}
26 changes: 13 additions & 13 deletions src/pkg/layout/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Components struct {
var ErrNotLoaded = fmt.Errorf("not loaded")

// Archive archives a component.
func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool) (err error) {
func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool) error {
name := component.Name
if _, ok := c.Dirs[name]; !ok {
return &fs.PathError{
Expand Down Expand Up @@ -75,7 +75,7 @@ func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool)
}

// Unarchive unarchives a component.
func (c *Components) Unarchive(component v1alpha1.ZarfComponent) (err error) {
func (c *Components) Unarchive(component v1alpha1.ZarfComponent) error {
name := component.Name
tb, ok := c.Tarballs[name]
if !ok {
Expand Down Expand Up @@ -138,7 +138,7 @@ func (c *Components) Unarchive(component v1alpha1.ZarfComponent) (err error) {
}

// Create creates a new component directory structure.
func (c *Components) Create(component v1alpha1.ZarfComponent) (cp *ComponentPaths, err error) {
func (c *Components) Create(component v1alpha1.ZarfComponent) (*ComponentPaths, error) {
name := component.Name

_, ok := c.Tarballs[name]
Expand All @@ -150,41 +150,41 @@ func (c *Components) Create(component v1alpha1.ZarfComponent) (cp *ComponentPath
}
}

if err = helpers.CreateDirectory(c.Base, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(c.Base, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}

base := filepath.Join(c.Base, name)

if err = helpers.CreateDirectory(base, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(base, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}

cp = &ComponentPaths{
cp := &ComponentPaths{
Base: base,
}

cp.Temp = filepath.Join(base, TempDir)
if err = helpers.CreateDirectory(cp.Temp, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Temp, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}

if len(component.Files) > 0 {
cp.Files = filepath.Join(base, FilesDir)
if err = helpers.CreateDirectory(cp.Files, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Files, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
}

if len(component.Charts) > 0 {
cp.Charts = filepath.Join(base, ChartsDir)
if err = helpers.CreateDirectory(cp.Charts, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Charts, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
for _, chart := range component.Charts {
cp.Values = filepath.Join(base, ValuesDir)
if len(chart.ValuesFiles) > 0 {
if err = helpers.CreateDirectory(cp.Values, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Values, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
break
Expand All @@ -194,21 +194,21 @@ func (c *Components) Create(component v1alpha1.ZarfComponent) (cp *ComponentPath

if len(component.Repos) > 0 {
cp.Repos = filepath.Join(base, ReposDir)
if err = helpers.CreateDirectory(cp.Repos, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Repos, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
}

if len(component.Manifests) > 0 {
cp.Manifests = filepath.Join(base, ManifestsDir)
if err = helpers.CreateDirectory(cp.Manifests, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Manifests, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
}

if len(component.DataInjections) > 0 {
cp.DataInjections = filepath.Join(base, DataInjectionsDir)
if err = helpers.CreateDirectory(cp.DataInjections, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.DataInjections, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/pkg/layout/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ func New(baseDir string) *PackagePaths {

// ReadZarfYAML reads a zarf.yaml file into memory,
// checks if it's using the legacy layout, and migrates deprecated component configs.
func (pp *PackagePaths) ReadZarfYAML() (pkg v1alpha1.ZarfPackage, warnings []string, err error) {
func (pp *PackagePaths) ReadZarfYAML() (v1alpha1.ZarfPackage, []string, error) {
var pkg v1alpha1.ZarfPackage

if err := utils.ReadYaml(pp.ZarfYAML, &pkg); err != nil {
return v1alpha1.ZarfPackage{}, nil, fmt.Errorf("unable to read zarf.yaml: %w", err)
}

warnings := make([]string, 0)
if pp.IsLegacyLayout() {
warnings = append(warnings, "Detected deprecated package layout, migrating to new layout - support for this package will be dropped in v1.0.0")
}
Expand All @@ -74,7 +77,7 @@ func (pp *PackagePaths) ReadZarfYAML() (pkg v1alpha1.ZarfPackage, warnings []str
}

// MigrateLegacy migrates a legacy package layout to the new layout.
func (pp *PackagePaths) MigrateLegacy() (err error) {
func (pp *PackagePaths) MigrateLegacy() error {
var pkg v1alpha1.ZarfPackage
base := pp.Base

Expand Down
Loading

0 comments on commit 868c7c0

Please sign in to comment.