-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: print the correct field path when spec.api.publicName is missing
We introduce a field-mapping layer to help us map v1alpha2 -> v1alpha3 and vice-versa
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters