Skip to content

Commit

Permalink
CAPIProvider: Always provide a name for UI and -o wide (#777)
Browse files Browse the repository at this point in the history
Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev authored Oct 11, 2024
1 parent a620e34 commit 9f6a4bf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
5 changes: 4 additions & 1 deletion api/v1alpha1/capiprovider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ type CAPIProviderStatus struct {
// +kubebuilder:default={CLUSTER_TOPOLOGY:"true",EXP_CLUSTER_RESOURCE_SET:"true",EXP_MACHINE_POOL: "true"}
Variables map[string]string `json:"variables,omitempty"`

// Name reflects actual provider name, which will be visible to users in 'kubectl get capiproviders -A -o wide'
Name string `json:"name,omitempty"`

operatorv1.ProviderStatus `json:",inline"`
}

Expand All @@ -123,7 +126,7 @@ type CAPIProviderStatus struct {
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Type",type="string",JSONPath=".spec.type"
// +kubebuilder:printcolumn:name="ProviderName",type="string",JSONPath=".spec.name"
// +kubebuilder:printcolumn:name="ProviderName",type="string",JSONPath=".status.name"
// +kubebuilder:printcolumn:name="InstalledVersion",type="string",JSONPath=".status.installedVersion"
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase"
// +kubebuilder:validation:XValidation:message="CAPI Provider type should always be set.",rule="has(self.spec.type)"
Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/capiprovider_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"cmp"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
Expand Down Expand Up @@ -74,6 +76,11 @@ func (b *CAPIProvider) SetVariables(v map[string]string) {
b.Status.Variables = v
}

// SetProviderName updates provider name based on spec field or metadata.name.
func (b *CAPIProvider) SetProviderName() {
b.Status.Name = cmp.Or(b.Spec.Name, b.Name)
}

// SetPhase updates the Phase field in the CAPIProvider status.
func (b *CAPIProvider) SetPhase(p Phase) {
b.Status.Phase = p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
- jsonPath: .spec.type
name: Type
type: string
- jsonPath: .spec.name
- jsonPath: .status.name
name: ProviderName
type: string
- jsonPath: .status.installedVersion
Expand Down Expand Up @@ -3065,6 +3065,10 @@ spec:
description: InstalledVersion is the version of the provider that
is installed.
type: string
name:
description: Name reflects actual provider name, which will be visible
to users in 'kubectl get capiproviders -A -o wide'
type: string
observedGeneration:
description: ObservedGeneration is the latest generation observed
by the controller.
Expand Down
6 changes: 5 additions & 1 deletion config/crd/bases/turtles-capi.cattle.io_capiproviders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
- jsonPath: .spec.type
name: Type
type: string
- jsonPath: .spec.name
- jsonPath: .status.name
name: ProviderName
type: string
- jsonPath: .status.installedVersion
Expand Down Expand Up @@ -3065,6 +3065,10 @@ spec:
description: InstalledVersion is the version of the provider that
is installed.
type: string
name:
description: Name reflects actual provider name, which will be visible
to users in 'kubectl get capiproviders -A -o wide'
type: string
observedGeneration:
description: ObservedGeneration is the latest generation observed
by the controller.
Expand Down
27 changes: 27 additions & 0 deletions internal/controllers/capiprovider_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,33 @@ var _ = Describe("Reconcile CAPIProvider", func() {
})))
})

It("Should inherit docker provider name", func() {
provider := &turtlesv1.CAPIProvider{ObjectMeta: metav1.ObjectMeta{
Name: "docker",
Namespace: ns.Name,
}, Spec: turtlesv1.CAPIProviderSpec{
Type: turtlesv1.Infrastructure,
}}
Expect(cl.Create(ctx, provider)).ToNot(HaveOccurred())

Eventually(Object(provider)).Should(
HaveField("Status.Name", Equal(provider.Name)))
})

It("Should inherit docker provider name from the spec", func() {
provider := &turtlesv1.CAPIProvider{ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns.Name,
}, Spec: turtlesv1.CAPIProviderSpec{
Name: "docker",
Type: turtlesv1.Infrastructure,
}}
Expect(cl.Create(ctx, provider)).ToNot(HaveOccurred())

Eventually(Object(provider)).Should(
HaveField("Status.Name", Equal(provider.Spec.Name)))
})

It("Should update infrastructure docker provider version and secret content from CAPI Provider change", func() {
provider := &turtlesv1.CAPIProvider{ObjectMeta: metav1.ObjectMeta{
Name: "docker",
Expand Down
2 changes: 2 additions & 0 deletions internal/sync/provider_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (s *ProviderSync) SyncObjects() {
}

func (s *ProviderSync) syncStatus() {
s.DefaultSynchronizer.Source.SetProviderName()

switch {
case conditions.IsTrue(s.Source, operatorv1.ProviderInstalledCondition):
s.Source.SetPhase(turtlesv1.Ready)
Expand Down

0 comments on commit 9f6a4bf

Please sign in to comment.