1
0
mirror of synced 2026-02-06 19:05:55 +00:00

Add upgrade CRDs policy to create helmrelease cmd

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
Stefan Prodan
2021-04-23 10:17:49 +03:00
parent 9164914d16
commit 5ab8dd2557
5 changed files with 125 additions and 7 deletions

60
internal/flags/crds.go Normal file
View File

@@ -0,0 +1,60 @@
/*
Copyright 2021 The Flux 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 flags
import (
"fmt"
"strings"
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/fluxcd/flux2/internal/utils"
)
var supportedCRDsPolicies = []string{
string(helmv2.Skip),
string(helmv2.Create),
string(helmv2.CreateReplace),
}
type CRDsPolicy string
func (a *CRDsPolicy) String() string {
return string(*a)
}
func (a *CRDsPolicy) Set(str string) error {
if strings.TrimSpace(str) == "" {
return fmt.Errorf("no upgrade CRDs policy given, must be one of: %s",
strings.Join(supportedCRDsPolicies, ", "))
}
if !utils.ContainsItemString(supportedCRDsPolicies, str) {
return fmt.Errorf("unsupported upgrade CRDs policy '%s', must be one of: %s",
str, strings.Join(supportedCRDsPolicies, ", "))
}
*a = CRDsPolicy(str)
return nil
}
func (a *CRDsPolicy) Type() string {
return "crds"
}
func (a *CRDsPolicy) Description() string {
return fmt.Sprintf("upgrade CRDs policy, available options are: (%s)", strings.Join(supportedCRDsPolicies, ", "))
}

View File

@@ -0,0 +1,45 @@
/*
Copyright 2021 The Flux 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 flags
import (
"testing"
)
func TestCRDsPolicy_Set(t *testing.T) {
tests := []struct {
name string
str string
expect string
expectErr bool
}{
{"supported", "CreateReplace", "CreateReplace", false},
{"unsupported", "createreplace", "", true},
{"empty", "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var a CRDsPolicy
if err := a.Set(tt.str); (err != nil) != tt.expectErr {
t.Errorf("Set() error = %v, expectErr %v", err, tt.expectErr)
}
if str := a.String(); str != tt.expect {
t.Errorf("Set() = %v, expect %v", str, tt.expect)
}
})
}
}