mirror of https://github.com/helm/helm
commit
3a1719e4e1
@ -0,0 +1,83 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"helm.sh/helm/v4/pkg/action"
|
||||
)
|
||||
|
||||
func addDryRunFlag(cmd *cobra.Command) {
|
||||
// --dry-run options with expected outcome:
|
||||
// - Not set means no dry run and server is contacted.
|
||||
// - Set with no value, a value of client, or a value of true and the server is not contacted
|
||||
// - Set with a value of false, none, or false and the server is contacted
|
||||
// The true/false part is meant to reflect some legacy behavior while none is equal to "".
|
||||
f := cmd.Flags()
|
||||
f.String(
|
||||
"dry-run",
|
||||
"none",
|
||||
`simulates the operation without persisting changes. Must be one of: "none" (default), "client", or "server". '--dry-run=none' executes the operation normally and persists changes (no simulation). '--dry-run=client' simulates the operation client-side only and avoids cluster connections. '--dry-run=server' simulates the operation on the server, requiring cluster connectivity.`)
|
||||
f.Lookup("dry-run").NoOptDefVal = "unset"
|
||||
}
|
||||
|
||||
// Determine the `action.DryRunStrategy` given -dry-run=<value>` flag (or absence of)
|
||||
// Legacy usage of the flag: boolean values, and `--dry-run` (without value) are supported, and log warnings emitted
|
||||
func cmdGetDryRunFlagStrategy(cmd *cobra.Command, isTemplate bool) (action.DryRunStrategy, error) {
|
||||
|
||||
f := cmd.Flag("dry-run")
|
||||
v := f.Value.String()
|
||||
|
||||
switch v {
|
||||
case f.NoOptDefVal:
|
||||
slog.Warn(`--dry-run is deprecated and should be replaced with '--dry-run=client'`)
|
||||
return action.DryRunClient, nil
|
||||
case string(action.DryRunClient):
|
||||
return action.DryRunClient, nil
|
||||
case string(action.DryRunServer):
|
||||
return action.DryRunServer, nil
|
||||
case string(action.DryRunNone):
|
||||
if isTemplate {
|
||||
// Special case hack for `helm template`, which is always a dry run
|
||||
return action.DryRunNone, fmt.Errorf(`invalid dry-run value (%q). Must be "server" or "client"`, v)
|
||||
}
|
||||
return action.DryRunNone, nil
|
||||
}
|
||||
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return action.DryRunNone, fmt.Errorf(`invalid dry-run value (%q). Must be "none", "server", or "client"`, v)
|
||||
}
|
||||
|
||||
if isTemplate && !b {
|
||||
// Special case for `helm template`, which is always a dry run
|
||||
return action.DryRunNone, fmt.Errorf(`invalid dry-run value (%q). Must be "server" or "client"`, v)
|
||||
}
|
||||
|
||||
result := action.DryRunNone
|
||||
if b {
|
||||
result = action.DryRunClient
|
||||
}
|
||||
slog.Warn(fmt.Sprintf(`boolean '--dry-run=%v' flag is deprecated and must be replaced with '--dry-run=%s'`, v, result))
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Binary file not shown.
@ -0,0 +1,116 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package release
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"helm.sh/helm/v4/pkg/chart"
|
||||
v1release "helm.sh/helm/v4/pkg/release/v1"
|
||||
)
|
||||
|
||||
var NewAccessor func(rel Releaser) (Accessor, error) = newDefaultAccessor //nolint:revive
|
||||
|
||||
var NewHookAccessor func(rel Hook) (HookAccessor, error) = newDefaultHookAccessor //nolint:revive
|
||||
|
||||
func newDefaultAccessor(rel Releaser) (Accessor, error) {
|
||||
switch v := rel.(type) {
|
||||
case v1release.Release:
|
||||
return &v1Accessor{&v}, nil
|
||||
case *v1release.Release:
|
||||
return &v1Accessor{v}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported release type: %T", rel)
|
||||
}
|
||||
}
|
||||
|
||||
func newDefaultHookAccessor(hook Hook) (HookAccessor, error) {
|
||||
switch h := hook.(type) {
|
||||
case v1release.Hook:
|
||||
return &v1HookAccessor{&h}, nil
|
||||
case *v1release.Hook:
|
||||
return &v1HookAccessor{h}, nil
|
||||
default:
|
||||
return nil, errors.New("unsupported release hook type")
|
||||
}
|
||||
}
|
||||
|
||||
type v1Accessor struct {
|
||||
rel *v1release.Release
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Name() string {
|
||||
return a.rel.Name
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Namespace() string {
|
||||
return a.rel.Namespace
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Version() int {
|
||||
return a.rel.Version
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Hooks() []Hook {
|
||||
var hooks = make([]Hook, len(a.rel.Hooks))
|
||||
for i, h := range a.rel.Hooks {
|
||||
hooks[i] = h
|
||||
}
|
||||
return hooks
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Manifest() string {
|
||||
return a.rel.Manifest
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Notes() string {
|
||||
return a.rel.Info.Notes
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Labels() map[string]string {
|
||||
return a.rel.Labels
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Chart() chart.Charter {
|
||||
return a.rel.Chart
|
||||
}
|
||||
|
||||
func (a *v1Accessor) Status() string {
|
||||
return a.rel.Info.Status.String()
|
||||
}
|
||||
|
||||
func (a *v1Accessor) ApplyMethod() string {
|
||||
return a.rel.ApplyMethod
|
||||
}
|
||||
|
||||
func (a *v1Accessor) DeployedAt() time.Time {
|
||||
return a.rel.Info.LastDeployed
|
||||
}
|
||||
|
||||
type v1HookAccessor struct {
|
||||
hook *v1release.Hook
|
||||
}
|
||||
|
||||
func (a *v1HookAccessor) Path() string {
|
||||
return a.hook.Path
|
||||
}
|
||||
|
||||
func (a *v1HookAccessor) Manifest() string {
|
||||
return a.hook.Manifest
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package release
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"helm.sh/helm/v4/pkg/release/common"
|
||||
rspb "helm.sh/helm/v4/pkg/release/v1"
|
||||
)
|
||||
|
||||
func TestNewDefaultAccessor(t *testing.T) {
|
||||
// Testing the default implementation rather than NewAccessor which can be
|
||||
// overridden by developers.
|
||||
is := assert.New(t)
|
||||
|
||||
// Create release
|
||||
info := &rspb.Info{Status: common.StatusDeployed, LastDeployed: time.Now().Add(1000)}
|
||||
labels := make(map[string]string)
|
||||
labels["foo"] = "bar"
|
||||
rel := &rspb.Release{
|
||||
Name: "happy-cats",
|
||||
Version: 2,
|
||||
Info: info,
|
||||
Labels: labels,
|
||||
Namespace: "default",
|
||||
ApplyMethod: "csa",
|
||||
}
|
||||
|
||||
// newDefaultAccessor should not be called directly Instead, NewAccessor should be
|
||||
// called and it will call NewDefaultAccessor. NewAccessor can be changed to a
|
||||
// non-default accessor by a user so the test calls the default implementation.
|
||||
// The accessor provides a means to access data on resources that are different types
|
||||
// but have the same interface. Instead of properties, methods are used to access
|
||||
// information. Structs with properties are useful in Go when it comes to marshalling
|
||||
// and unmarshalling data (e.g. coming and going from JSON or YAML). But, structs
|
||||
// can't be used with interfaces. The accessors enable access to the underlying data
|
||||
// in a manner that works with Go interfaces.
|
||||
accessor, err := newDefaultAccessor(rel)
|
||||
is.NoError(err)
|
||||
|
||||
// Verify information
|
||||
is.Equal(rel.Name, accessor.Name())
|
||||
is.Equal(rel.Namespace, accessor.Namespace())
|
||||
is.Equal(rel.Version, accessor.Version())
|
||||
is.Equal(rel.ApplyMethod, accessor.ApplyMethod())
|
||||
is.Equal(rel.Labels, accessor.Labels())
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package release
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"helm.sh/helm/v4/pkg/chart"
|
||||
)
|
||||
|
||||
type Releaser interface{}
|
||||
|
||||
type Hook interface{}
|
||||
|
||||
type Accessor interface {
|
||||
Name() string
|
||||
Namespace() string
|
||||
Version() int
|
||||
Hooks() []Hook
|
||||
Manifest() string
|
||||
Notes() string
|
||||
Labels() map[string]string
|
||||
Chart() chart.Charter
|
||||
Status() string
|
||||
ApplyMethod() string
|
||||
DeployedAt() time.Time
|
||||
}
|
||||
|
||||
type HookAccessor interface {
|
||||
Path() string
|
||||
Manifest() string
|
||||
}
|
||||
Loading…
Reference in new issue