Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Fix empty flannel backend configuration expectation on an existing cluster #2601

Merged
merged 1 commit into from
Sep 1, 2021
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
3 changes: 3 additions & 0 deletions lib/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,9 @@ var (

// KubeControllerManagerPort specifies the default controller manager healthz port
KubeControllerManagerPort = 10257

// FlannelBackend specifies the default flannel backend
FlannelBackend = "vxlan"
)

// HookSecurityContext returns default securityContext for hook pods
Expand Down
4 changes: 2 additions & 2 deletions lib/ops/opsservice/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1444,9 +1444,9 @@ func (s *site) addClusterConfig(config clusterconfig.Interface, overrideArgs map
if globalConfig.HighAvailability != nil && *globalConfig.HighAvailability {
args = append(args, "--high-availability")
}
if globalConfig.FlannelBackend != "" {
if globalConfig.FlannelBackend != nil {
args = append(args,
fmt.Sprintf("--flannel-backend=%v", globalConfig.FlannelBackend))
fmt.Sprintf("--flannel-backend=%v", *globalConfig.FlannelBackend))
}
return args
}
Expand Down
13 changes: 2 additions & 11 deletions lib/ops/resources/gravity/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,7 @@ func (r configCollection) WriteText(w io.Writer) error {
fmt.Fprintf(t, "%v\n", string(config.Config))
}
config := r.GetGlobalConfig()
if len(config.FlannelBackend) != 0 {
fmt.Fprintf(t, "Flannel Backend:\t%v\n", config.FlannelBackend)
}
fmt.Fprintf(t, "Flannel Backend:\t%v\n", utils.FormatStringPtrWithDefault(config.FlannelBackend, defaults.FlannelBackend))
if len(config.PodCIDR) != 0 {
fmt.Fprintf(t, "Pod IP Range:\t%v\n", config.PodCIDR)
}
Expand All @@ -685,7 +683,7 @@ func (r configCollection) WriteText(w io.Writer) error {
if len(config.FeatureGates) != 0 {
fmt.Fprintf(t, "Feature Gates:\t%v\n", formatFeatureGates(config.FeatureGates))
}
fmt.Fprintf(t, "High Availability:\t%v\n", formatBoolPtr(config.HighAvailability))
fmt.Fprintf(t, "High Availability:\t%v\n", utils.FormatBoolPtr(config.HighAvailability))
displayCloudConfig := config.CloudProvider != "" || config.CloudConfig != ""
if displayCloudConfig {
common.PrintCustomTableHeader(t, []string{"Cloud"}, "-")
Expand Down Expand Up @@ -742,13 +740,6 @@ func formatFeatureGates(features map[string]bool) string {
return strings.Join(result, ",")
}

func formatBoolPtr(ptr *bool) string {
if ptr == nil {
return "<unset>"
}
return fmt.Sprint(*ptr)
}

type operationsCollection struct {
operations []storage.Operation
}
Expand Down
13 changes: 8 additions & 5 deletions lib/storage/clusterconfig/clusterconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ func (r Resource) Merge(other Resource) Resource {
}
}
// Changing cloud provider is not supported
if other.Spec.Global.FlannelBackend != "" {
r.Spec.Global.FlannelBackend = other.Spec.Global.FlannelBackend
if other.Spec.Global.FlannelBackend != nil {
if r.Spec.Global.FlannelBackend == nil {
r.Spec.Global.FlannelBackend = new(string)
}
*r.Spec.Global.FlannelBackend = *other.Spec.Global.FlannelBackend
}
if other.Spec.Global.PodCIDR != "" {
r.Spec.Global.PodCIDR = other.Spec.Global.PodCIDR
Expand Down Expand Up @@ -279,7 +282,7 @@ func (r Global) IsEmpty() bool {
r.ServiceNodePortRange == "" &&
r.ProxyPortRange == "" &&
r.HighAvailability == nil &&
r.FlannelBackend == "" &&
r.FlannelBackend == nil &&
len(r.FeatureGates) == 0
}

Expand Down Expand Up @@ -313,8 +316,8 @@ type Global struct {
FeatureGates map[string]bool `json:"featureGates,omitempty"`
// HighAvailability enables high availability mode for Kubernetes.
HighAvailability *bool `json:"highAvailability,omitempty"`
// FlannelBackend specifies the backend to pair with flannel.
FlannelBackend string `json:"flannelBackend,omitempty"`
// FlannelBackend optionally specifies the backend to pair with flannel.
FlannelBackend *string `json:"flannelBackend,omitempty"`
}

// specSchemaTemplate is JSON schema for the cluster configuration resource
Expand Down
11 changes: 6 additions & 5 deletions lib/storage/clusterconfig/clusterconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/gravitational/gravity/lib/constants"
"github.com/gravitational/gravity/lib/defaults"
"github.com/gravitational/gravity/lib/storage"
"github.com/gravitational/gravity/lib/utils"

teleservices "github.com/gravitational/teleport/lib/services"
"github.com/gravitational/trace"
Expand Down Expand Up @@ -187,7 +188,7 @@ spec:
},
PodSubnetSize: "26",
HighAvailability: newBoolPtr(true),
FlannelBackend: "vxlan",
FlannelBackend: utils.StringPtr("vxlan"),
},
},
},
Expand Down Expand Up @@ -246,7 +247,7 @@ func (*S) TestMergesClusterConfiguration(c *C) {
"feature2": false,
},
HighAvailability: newBoolPtr(true),
FlannelBackend: "vxlan",
FlannelBackend: utils.StringPtr("vxlan"),
},
},
},
Expand All @@ -266,7 +267,7 @@ func (*S) TestMergesClusterConfiguration(c *C) {
"feature2": false,
},
HighAvailability: newBoolPtr(true),
FlannelBackend: "vxlan",
FlannelBackend: utils.StringPtr("vxlan"),
},
},
},
Expand Down Expand Up @@ -294,7 +295,7 @@ address: 10.0.0.1
"feature1": true,
"feature2": false,
},
FlannelBackend: "vxlan",
FlannelBackend: utils.StringPtr("vxlan"),
},
},
},
Expand Down Expand Up @@ -341,7 +342,7 @@ address: 10.0.0.1
"feature1": true,
"feature3": true,
},
FlannelBackend: "vxlan",
FlannelBackend: utils.StringPtr("vxlan"),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion lib/update/clusterconfig/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func shouldUpdateNodes(clusterConfig clusterconfig.Interface, numWorkerNodes int
config := clusterConfig.GetGlobalConfig()
hasComponentUpdate := len(config.FeatureGates) != 0
hasCIDRUpdate := len(config.PodCIDR) != 0 || len(config.ServiceCIDR) != 0 || config.PodSubnetSize != ""
hasFlannelUpdate := config.FlannelBackend != ""
hasFlannelUpdate := config.FlannelBackend != nil
return !clusterConfig.GetKubeletConfig().IsEmpty() || hasComponentUpdate || hasCIDRUpdate || hasFlannelUpdate
}

Expand Down
25 changes: 25 additions & 0 deletions lib/utils/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,38 @@ func BoolPtr(v bool) *bool {
return &v
}

// FormatBoolPtr formats the bool pointer value for output
func FormatBoolPtr(v *bool) string {
if v == nil {
return "<unset>"
}
return fmt.Sprint(*v)
}

// DurationPtr returns a pointer to the provided duration value
func DurationPtr(v time.Duration) *teleservices.Duration {
d := teleservices.NewDuration(v)
return &d
}

// StringValue returns the string value in v or an empty string if it's nil
func StringValue(v *string) string {
if v != nil {
return *v
}
return ""
}

// StringPtr returns a pointer to the provided string
func StringPtr(s string) *string {
return &s
}

// FormatStringPtrWithDefault formats the string pointer value for output.
// If the pointer value is nil, the specified default is used
func FormatStringPtrWithDefault(v *string, def string) string {
if v == nil {
return def
}
return *v
}
9 changes: 7 additions & 2 deletions lib/validate/clusterconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ func ClusterConfiguration(existing, update clusterconfig.Interface) error {
return trace.Wrap(err)
}

if !isSupportedBackend(newGlobalConfig.FlannelBackend) {
return trace.BadParameter("unsupported flannel backend was specified: %v", newGlobalConfig.FlannelBackend)
if newGlobalConfig.FlannelBackend != nil {
if *newGlobalConfig.FlannelBackend == "" {
return trace.BadParameter("flannel backend cannot be reset")
}
if !isSupportedBackend(*newGlobalConfig.FlannelBackend) {
return trace.BadParameter("unsupported flannel backend was specified: %v", *newGlobalConfig.FlannelBackend)
}
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion tool/gravity/cli/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/gravitational/gravity/lib/ops/resources"
"github.com/gravitational/gravity/lib/storage"
"github.com/gravitational/gravity/lib/storage/clusterconfig"
"github.com/gravitational/gravity/lib/utils"

"gopkg.in/check.v1"
)
Expand Down Expand Up @@ -77,7 +78,7 @@ spec:
ServiceCIDR: "100.200.0.0/16",
PodCIDR: "100.96.0.0/16",
PodSubnetSize: "26",
FlannelBackend: "vxlan",
FlannelBackend: utils.StringPtr("vxlan"),
CloudConfig: `[global]
node-tags=example-cluster
multizone="true"
Expand Down