Skip to content

Commit

Permalink
Relax mc validations in the CLI to avoid errors
Browse files Browse the repository at this point in the history
Followup to #13061

When the CLI queried for Links in the cluster, we were assuming the CLI version matched the linkerd version in the cluster, and so we validated that all fields should be present.

This change relaxes that assumption by setting defaults for the new fields `failureThreshold` and `timeout`, so we don't get an error for example when trying to run `linkerd multicluster uninstall` on a cluster with an older `Link` CRD.
  • Loading branch information
alpeb committed Oct 7, 2024
1 parent a76efe0 commit 9f7d8ef
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions pkg/multicluster/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type (
Selector metav1.LabelSelector
RemoteDiscoverySelector metav1.LabelSelector
}

ErrFieldMissing struct {
Field string
}
)

// LinkGVR is the Group Version and Resource of the Link custom resource.
Expand All @@ -63,6 +67,10 @@ func (ps ProbeSpec) String() string {
return fmt.Sprintf("ProbeSpec: {path: %s, port: %d, period: %s}", ps.Path, ps.Port, ps.Period)
}

func (e *ErrFieldMissing) Error() string {
return fmt.Sprintf("Field '%s' is missing", e.Field)
}

// NewLink parses an unstructured link.multicluster.linkerd.io resource and
// converts it to a structured internal representation.
func NewLink(u unstructured.Unstructured) (Link, error) {
Expand Down Expand Up @@ -326,7 +334,13 @@ func newProbeSpec(obj map[string]interface{}) (ProbeSpec, error) {

failureThresholdStr, err := stringField(obj, "failureThreshold")
if err != nil {
return ProbeSpec{}, err
var efm *ErrFieldMissing
if errors.As(err, &efm) {
// older Links might not have this field
failureThresholdStr = fmt.Sprint(DefaultFailureThreshold)
} else {
return ProbeSpec{}, err
}
}
failureThreshold, err := strconv.ParseUint(failureThresholdStr, 10, 32)
if err != nil {
Expand All @@ -335,7 +349,13 @@ func newProbeSpec(obj map[string]interface{}) (ProbeSpec, error) {

timeoutStr, err := stringField(obj, "timeout")
if err != nil {
return ProbeSpec{}, err
var efm *ErrFieldMissing
if errors.As(err, &efm) {
// older Links might not have this field
timeoutStr = DefaultProbeTimeout
} else {
return ProbeSpec{}, err
}
}
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
Expand Down Expand Up @@ -368,7 +388,7 @@ func newProbeSpec(obj map[string]interface{}) (ProbeSpec, error) {
func stringField(obj map[string]interface{}, key string) (string, error) {
value, ok := obj[key]
if !ok {
return "", fmt.Errorf("Field '%s' is missing", key)
return "", &ErrFieldMissing{Field: key}
}
str, ok := value.(string)
if !ok {
Expand Down

0 comments on commit 9f7d8ef

Please sign in to comment.