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

Spec validation #109

Merged
merged 5 commits into from
May 19, 2020
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
56 changes: 54 additions & 2 deletions api/v1alpha1/instancegroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1
import (
"strings"

"github.com/keikoproj/instance-manager/controllers/common"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -44,13 +45,27 @@ const (
ReconcileReady ReconcileState = "Ready"
ReconcileErr ReconcileState = "Error"

LifecycleStateNormal = "normal"
LifecycleStateSpot = "spot"
LifecycleStateNormal = "normal"
LifecycleStateSpot = "spot"
CRDStrategyName = "crd"
RollingUpdateStrategyName = "rollingupdate"
EKSProvisionerName = "eks"
EKSManagedProvisionerName = "eks-managed"

NodesReady InstanceGroupConditionType = "NodesReady"
)

var (
Strategies = []string{"crd", "rollingupdate", "managed"}
Provisioners = []string{"eks", "eks-managed"}

DefaultRollingUpdateStrategy = &RollingUpdateStrategy{
MaxUnavailable: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 1,
},
}

GroupVersionResource = schema.GroupVersionResource{
Group: "instancemgr.keikoproj.io",
Version: "v1alpha1",
Expand Down Expand Up @@ -247,6 +262,43 @@ func (c *EKSConfiguration) Validate() error {
}
return nil
}
func (s *InstanceGroupSpec) Validate() error {
if !common.ContainsEqualFold(Provisioners, s.Provisioner) {
return errors.Errorf("validation failed, provisioner '%v' is invalid", s.Provisioner)
}

if strings.EqualFold(s.Provisioner, EKSProvisionerName) {
if err := s.EKSSpec.EKSConfiguration.Validate(); err != nil {
return err
}
}

// TODO: Add validation for EKSManagedProvisioner

if s.AwsUpgradeStrategy.Type == "" {
s.AwsUpgradeStrategy.Type = RollingUpdateStrategyName
}

if !common.ContainsEqualFold(Strategies, s.AwsUpgradeStrategy.Type) {
return errors.Errorf("validation failed, strategy '%v' is invalid", s.AwsUpgradeStrategy.Type)
}

if strings.EqualFold(s.AwsUpgradeStrategy.Type, CRDStrategyName) && s.AwsUpgradeStrategy.CRDType == nil {
return errors.Errorf("validation failed, strategy.crd is required")
}

if strings.EqualFold(s.AwsUpgradeStrategy.Type, CRDStrategyName) && s.AwsUpgradeStrategy.CRDType != nil {
if err := s.AwsUpgradeStrategy.CRDType.Validate(); err != nil {
return err
}
}

if strings.EqualFold(s.AwsUpgradeStrategy.Type, RollingUpdateStrategyName) && s.AwsUpgradeStrategy.RollingUpdateType == nil {
s.AwsUpgradeStrategy.RollingUpdateType = DefaultRollingUpdateStrategy
}

return nil
}
func (c *EKSConfiguration) GetRoleName() string {
return c.ExistingRoleName
}
Expand Down
10 changes: 10 additions & 0 deletions controllers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func ContainsString(slice []string, s string) bool {
return false
}

// ContainsString returns true if a given slice 'slice' contains string 's', otherwise return false
func ContainsEqualFold(slice []string, s string) bool {
for _, item := range slice {
if strings.EqualFold(item, s) {
return true
}
}
return false
}

func StringSliceEquals(x, y []string) bool {
sort.Strings(x)
sort.Strings(y)
Expand Down
7 changes: 7 additions & 0 deletions controllers/instancegroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func (r *InstanceGroupReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
return ctrl.Result{}, err
}

if err = instanceGroup.Spec.Validate(); err != nil {
r.Log.Error(err, "reconcile failed")
instanceGroup.SetState(v1alpha1.ReconcileErr)
r.Update(context.Background(), instanceGroup)
return ctrl.Result{}, err
}

// Add Finalizer if not present, and set the initial state
finalizerName := fmt.Sprintf("finalizers.%v.instancegroups.keikoproj.io", instanceGroup.Spec.Provisioner)
r.SetFinalizer(instanceGroup, finalizerName)
Expand Down
8 changes: 2 additions & 6 deletions controllers/provisioners/eks/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ func (ctx *EksInstanceGroupContext) UpgradeNodes() error {
var (
instanceGroup = ctx.GetInstanceGroup()
strategy = ctx.GetUpgradeStrategy()
strategyType = strings.ToLower(strategy.GetType())
)

// process the upgrade strategy
switch strings.ToLower(strategy.GetType()) {
switch strategyType {
case kubeprovider.CRDStrategyName:
crdStrategy := strategy.GetCRDType()
if err := crdStrategy.Validate(); err != nil {
instanceGroup.SetState(v1alpha1.ReconcileErr)
return errors.Wrap(err, "failed to validate strategy spec")
}
ok, err := kubeprovider.ProcessCRDStrategy(ctx.KubernetesClient.KubeDynamic, instanceGroup)
if err != nil {
instanceGroup.SetState(v1alpha1.ReconcileErr)
Expand Down
40 changes: 21 additions & 19 deletions controllers/provisioners/eks/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@ import (

func TestUpgradeCRDStrategyValidation(t *testing.T) {
var (
g = gomega.NewGomegaWithT(t)
k = MockKubernetesClientSet()
ig = MockInstanceGroup()
cr = MockCustomResourceSpec()
crd = MockCustomResourceDefinition()
asgMock = NewAutoScalingMocker()
iamMock = NewIamMocker()
g = gomega.NewGomegaWithT(t)
k = MockKubernetesClientSet()
ig = MockInstanceGroup()
cr = MockCustomResourceSpec()
crd = MockCustomResourceDefinition()
)

w := MockAwsWorker(asgMock, iamMock)
ctx := MockContext(ig, k, w)
config := ig.GetEKSConfiguration()

// assume initial state of modifying
ig.SetState(v1alpha1.ReconcileModifying)
config.Subnets = []string{"subnet-1"}
config.Image = "ami-1234"
config.NodeSecurityGroups = []string{"sg-12323"}
config.InstanceType = "m5.large"
config.KeyPairName = "myKey"

// get custom resource yaml
crYAML, err := yaml.Marshal(cr.Object)
Expand Down Expand Up @@ -79,25 +81,25 @@ func TestUpgradeCRDStrategyValidation(t *testing.T) {
shouldErr bool
expectedState v1alpha1.ReconcileState
}{
{input: valid, shouldErr: false, expectedState: v1alpha1.ReconcileModifying},
{input: missingName, shouldErr: true, expectedState: v1alpha1.ReconcileErr},
{input: missingSpec, shouldErr: true, expectedState: v1alpha1.ReconcileErr},
{input: missingConcurrency, shouldErr: false, expectedState: v1alpha1.ReconcileModifying},
{input: missingStatusPath, shouldErr: true, expectedState: v1alpha1.ReconcileErr},
{input: missingSuccessStr, shouldErr: true, expectedState: v1alpha1.ReconcileErr},
{input: missingFailureStr, shouldErr: true, expectedState: v1alpha1.ReconcileErr},
{input: valid, shouldErr: false},
{input: missingName, shouldErr: true},
{input: missingSpec, shouldErr: true},
{input: missingConcurrency, shouldErr: false},
{input: missingStatusPath, shouldErr: true},
{input: missingSuccessStr, shouldErr: true},
{input: missingFailureStr, shouldErr: true},
}

for i, tc := range tests {
t.Logf("#%v - %+v", i, tc.input.CRDType)
t.Logf("#%v - %+v", i, tc.input)
var errOccured bool
ig.SetUpgradeStrategy(tc.input)
err := ctx.UpgradeNodes()
err := ig.Spec.Validate()
if err != nil {
t.Log(err)
errOccured = true
}
g.Expect(errOccured).To(gomega.Equal(tc.shouldErr))
g.Expect(ctx.GetState()).To(gomega.Equal(tc.expectedState))
}
g.Expect(missingConcurrency.CRDType.ConcurrencyPolicy).To(gomega.Equal("forbid"))
}
Expand Down