Skip to content

Commit

Permalink
fix: print the correct field path when spec.api.publicName is missing
Browse files Browse the repository at this point in the history
We introduce a field-mapping layer to help us map v1alpha2 -> v1alpha3 and vice-versa
  • Loading branch information
justinsb committed Oct 11, 2024
1 parent 3a8da3d commit 8cded15
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
71 changes: 71 additions & 0 deletions pkg/apis/kops/fieldmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2024 The Kubernetes 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 kops

// Because we have multiple versions of the API, fields hvae different paths in different API versions.
// This file contains the mappings from the old paths to the new paths.

// These functions are somewhat of a work in progress, we are adding function mappings as we hit them.

// HumanPathForClusterField returns the path for a given v1alpha3 cluster field, as we should print it for users.
func HumanPathForClusterField(fieldPath string) string {
f := NewClusterField(fieldPath)
return f.HumanPath()
}

// ClusterField represents a field in the Cluster resource.
type ClusterField struct {
Path string
}

// NewClusterField creates a new ClusterField for the given path.
func NewClusterField(path string) *ClusterField {
return &ClusterField{Path: path}
}

// HumanPath returns the path for the field, as we should print it for users.
func (f *ClusterField) HumanPath() string {
return f.PathInV1Alpha2()
}

// PathInV1Alpha2 returns the path for the field in the v1alpha2 API.
func (f *ClusterField) PathInV1Alpha2() string {
for _, mapping := range clusterFieldMappings {
if mapping.V1Alpha3 == f.Path {
return mapping.V1Alpha2
}
}
return f.Path
}

// PathInV1Alpha3 returns the path for the field in the v1alpha3 API.
func (f *ClusterField) PathInV1Alpha3() string {
for _, mapping := range clusterFieldMappings {
if mapping.V1Alpha2 == f.Path {
return mapping.V1Alpha3
}
}
return f.Path
}

// clusterFieldMappings is a list of mappings from v1alpha2 field paths to v1alpha3 field paths.
var clusterFieldMappings = []struct {
V1Alpha2 string
V1Alpha3 string
}{
{V1Alpha2: "spec.masterPublicName", V1Alpha3: "spec.api.publicName"},
}
20 changes: 20 additions & 0 deletions pkg/apis/kops/fieldmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kops

import "testing"

func TestClusterFieldHumanPath(t *testing.T) {
grid := []struct {
In string
Expected string
}{
{In: "spec.api.publicName", Expected: "spec.masterPublicName"},
{In: "spec.api", Expected: "spec.api"},
}

for _, g := range grid {
actual := HumanPathForClusterField(g.In)
if actual != g.Expected {
t.Errorf("HumanPathForClusterField(%q) = %q; expected %q", g.In, actual, g.Expected)
}
}
}
2 changes: 1 addition & 1 deletion upup/pkg/fi/cloudup/metal/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Cloud) GetApiIngressStatus(cluster *kops.Cluster) ([]fi.ApiIngressStatu
var ret []fi.ApiIngressStatus
publicName := cluster.Spec.API.PublicName
if publicName == "" {
return ret, fmt.Errorf("spec.api.publicName must be set for bare metal")
return ret, fmt.Errorf("%s must be set for bare metal", kops.HumanPathForClusterField("spec.api.publicName"))
}
ip := net.ParseIP(publicName)
if ip == nil {
Expand Down

0 comments on commit 8cded15

Please sign in to comment.