Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Make seeds field type *int64
Browse files Browse the repository at this point in the history
  • Loading branch information
kragniz committed Mar 8, 2018
1 parent 1d02ea0 commit 92fbfe0
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/navigator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type CassandraClusterNodePool struct {
NodeSelector map[string]string
Rack string
Datacenter string
Seeds int64
Seeds *int64
}

type CassandraClusterStatus struct {
Expand Down
6 changes: 4 additions & 2 deletions pkg/apis/navigator/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime"

"github.com/jetstack/navigator/pkg/util"
)

const (
Expand All @@ -22,7 +24,7 @@ func SetDefaults_CassandraClusterNodePool(np *CassandraClusterNodePool) {
}

// default to 1 seed if not specified
if np.Seeds == 0 {
np.Seeds = 1
if np.Seeds == nil {
np.Seeds = util.Int64Ptr(1)
}
}
2 changes: 1 addition & 1 deletion pkg/apis/navigator/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type CassandraClusterNodePool struct {
// Seeds specifies the number of seed nodes to alocate in this nodepool. By
// default, 1 is selected.
// +optional
Seeds int64 `json:"seeds"`
Seeds *int64 `json:"seeds,omitempty"`
}

type CassandraClusterStatus struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/navigator/v1alpha1/zz_generated.conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func autoConvert_v1alpha1_CassandraClusterNodePool_To_navigator_CassandraCluster
out.NodeSelector = *(*map[string]string)(unsafe.Pointer(&in.NodeSelector))
out.Rack = in.Rack
out.Datacenter = in.Datacenter
out.Seeds = in.Seeds
out.Seeds = (*int64)(unsafe.Pointer(in.Seeds))
return nil
}

Expand All @@ -189,7 +189,7 @@ func autoConvert_navigator_CassandraClusterNodePool_To_v1alpha1_CassandraCluster
out.NodeSelector = *(*map[string]string)(unsafe.Pointer(&in.NodeSelector))
out.Rack = in.Rack
out.Datacenter = in.Datacenter
out.Seeds = in.Seeds
out.Seeds = (*int64)(unsafe.Pointer(in.Seeds))
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/navigator/v1alpha1/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ func (in *CassandraClusterNodePool) DeepCopyInto(out *CassandraClusterNodePool)
(*out)[key] = val
}
}
if in.Seeds != nil {
in, out := &in.Seeds, &out.Seeds
if *in == nil {
*out = nil
} else {
*out = new(int64)
**out = **in
}
}
return
}

Expand Down
27 changes: 15 additions & 12 deletions pkg/apis/navigator/validation/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ import (

func ValidateCassandraClusterNodePool(np *navigator.CassandraClusterNodePool, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if np.Seeds > np.Replicas {
allErrs = append(allErrs,
field.Invalid(
fldPath.Child("seeds"),
np.Seeds,
fmt.Sprintf("number of seeds cannot be greater than number of replicas (%d)", np.Replicas)),
)
}

if np.Seeds < 0 {
allErrs = append(allErrs,
field.Invalid(fldPath.Child("seeds"), np.Seeds, "number of seeds must be greater than or equal to 1"),
)
if np.Seeds != nil {
if *np.Seeds > np.Replicas {
allErrs = append(allErrs,
field.Invalid(
fldPath.Child("seeds"),
np.Seeds,
fmt.Sprintf("number of seeds cannot be greater than number of replicas (%d)", np.Replicas)),
)
}

if *np.Seeds < 1 {
allErrs = append(allErrs,
field.Invalid(fldPath.Child("seeds"), np.Seeds, "number of seeds must be greater than or equal to 1"),
)
}
}

return allErrs
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/navigator/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ func (in *CassandraClusterNodePool) DeepCopyInto(out *CassandraClusterNodePool)
(*out)[key] = val
}
}
if in.Seeds != nil {
in, out := &in.Seeds, &out.Seeds
if *in == nil {
*out = nil
} else {
*out = new(int64)
**out = **in
}
}
return
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/cassandra/seedlabeller/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *defaultSeedLabeller) labelSeedNodes(
}

// label first n as seeds
isSeed := i < np.Seeds
isSeed := i < *np.Seeds

desiredLabel := ""
if isSeed {
Expand Down
12 changes: 12 additions & 0 deletions pkg/controllers/cassandra/seedlabeller/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/jetstack/navigator/pkg/controllers/cassandra/seedlabeller"
"github.com/jetstack/navigator/pkg/controllers/cassandra/service/seedprovider"
casstesting "github.com/jetstack/navigator/pkg/controllers/cassandra/testing"
"github.com/jetstack/navigator/pkg/util"
)

func CheckSeedLabel(podName, seedLabelValue string, podNamespace string, t *testing.T, state *controllers.State) {
Expand Down Expand Up @@ -44,6 +45,9 @@ func TestSeedLabellerSync(t *testing.T) {
pod0ValueIncorrect := pod0LabelMissing.DeepCopy()
pod0ValueIncorrect.Labels[seedprovider.SeedLabelKey] = "blah"

clusterOneSeed := cluster.DeepCopy()
clusterOneSeed.Spec.NodePools[0].Seeds = util.Int64Ptr(1)

pod1 := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "cass-cassandra-1-RingNodes-1",
Expand Down Expand Up @@ -112,6 +116,14 @@ func TestSeedLabellerSync(t *testing.T) {
CheckSeedLabel(pod2.Name, "", pod2.Namespace, t, state)
},
},
"delete label if seed number decreased": {
kubeObjects: []runtime.Object{ss0, pod0, pod1, pod2},
navObjects: []runtime.Object{cluster},
cluster: clusterOneSeed,
assertions: func(t *testing.T, state *controllers.State) {
CheckSeedLabel(pod1.Name, "", pod1.Namespace, t, state)
},
},
}
for title, test := range tests {
t.Run(
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/cassandra/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
servicecql "github.com/jetstack/navigator/pkg/controllers/cassandra/service/cql"
serviceseedprovider "github.com/jetstack/navigator/pkg/controllers/cassandra/service/seedprovider"
"github.com/jetstack/navigator/pkg/controllers/cassandra/serviceaccount"
"github.com/jetstack/navigator/pkg/util"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -39,7 +40,7 @@ func ClusterForTest() *v1alpha1.CassandraCluster {
v1alpha1.CassandraClusterNodePool{
Name: "RingNodes",
Replicas: 3,
Seeds: 2,
Seeds: util.Int64Ptr(2),
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ func CalculateQuorum(num int32) int32 {
}
return (num / 2) + 1
}

func Int64Ptr(i int64) *int64 {
return &i
}

0 comments on commit 92fbfe0

Please sign in to comment.