Skip to content

Commit 0600133

Browse files
committed
refactor: use cluster namespaced name for cache key
- Update CacheParams to use types.NamespacedName for user-queryable cache key - Add newClientWithCluster function to accept cluster information - Update credentials check to pass cluster info for cache key - Remove fallback to credentials-based key, always use cluster namespaced name - Simplify Key() method to match CAPX approach
1 parent d341471 commit 0600133

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

pkg/webhook/preflight/nutanix/cache.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package nutanix
55

66
import (
7-
"fmt"
7+
k8stypes "k8s.io/apimachinery/pkg/types"
88

99
v4Converged "github.com/nutanix-cloud-native/prism-go-client/converged/v4"
1010
"github.com/nutanix-cloud-native/prism-go-client/environment/types"
@@ -23,6 +23,7 @@ var NutanixConvergedClientV4Cache = v4Converged.NewClientCache()
2323

2424
// CacheParams is the struct that implements ClientCacheParams interface from prism-go-client.
2525
type CacheParams struct {
26+
ClusterNamespacedName k8stypes.NamespacedName // Optional: cluster namespaced name for user-queryable cache key
2627
PrismManagementEndpoint *types.ManagementEndpoint
2728
}
2829

@@ -31,14 +32,7 @@ func (c *CacheParams) ManagementEndpoint() types.ManagementEndpoint {
3132
return *c.PrismManagementEndpoint
3233
}
3334

34-
// Key returns a unique key for the client cache based on the management endpoint.
35+
// Key returns a unique key for the client cache using the cluster namespaced name.
3536
func (c *CacheParams) Key() string {
36-
endpoint := c.PrismManagementEndpoint
37-
// Include address, username, password, and insecure flag to ensure unique keys per credential set
38-
return fmt.Sprintf("%s:%s:%s:%t",
39-
endpoint.Address.String(),
40-
endpoint.Username,
41-
endpoint.Password,
42-
endpoint.Insecure,
43-
)
37+
return c.ClusterNamespacedName.String()
4438
}

pkg/webhook/preflight/nutanix/checker.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88

99
"github.com/go-logr/logr"
10+
k8stypes "k8s.io/apimachinery/pkg/types"
1011
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1112
ctrl "sigs.k8s.io/controller-runtime"
1213
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -89,7 +90,16 @@ func (n *nutanixChecker) Init(
8990
// The configuration check must run first, because it initializes data used by all other checks,
9091
// and the credentials check second, because it initializes the Nutanix clients used by other checks.
9192
n.configurationCheckFactory(cd),
92-
n.credentialsCheckFactory(ctx, newClient, cd),
93+
n.credentialsCheckFactory(
94+
ctx,
95+
func(creds prismgoclient.Credentials) (client, error) { //nolint:contextcheck // ctx is captured from closure
96+
return newClient(creds, k8stypes.NamespacedName{
97+
Name: cluster.Name,
98+
Namespace: cluster.Namespace,
99+
})
100+
},
101+
cd,
102+
),
93103
n.prismCentralVersionCheckFactory(ctx, cd),
94104
}
95105

pkg/webhook/preflight/nutanix/clients.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
clustermgmtv4 "github.com/nutanix/ntnx-api-golang-clients/clustermgmt-go-client/v4/models/clustermgmt/v4/config"
1414
netv4 "github.com/nutanix/ntnx-api-golang-clients/networking-go-client/v4/models/networking/v4/config"
1515
vmmv4 "github.com/nutanix/ntnx-api-golang-clients/vmm-go-client/v4/models/vmm/v4/content"
16+
k8stypes "k8s.io/apimachinery/pkg/types"
1617

1718
prismgoclient "github.com/nutanix-cloud-native/prism-go-client"
1819
"github.com/nutanix-cloud-native/prism-go-client/converged"
@@ -251,14 +252,17 @@ func buildManagementEndpoint(credentials *prismgoclient.Credentials) (*types.Man
251252
}, nil
252253
}
253254

255+
// newClient creates a client with optional cluster information for cache key.
254256
func newClient(
255257
credentials prismgoclient.Credentials, //nolint:gocritic // hugeParam is fine
258+
clusterNamespacedName k8stypes.NamespacedName,
256259
) (client, error) {
257260
endpoint, err := buildManagementEndpoint(&credentials)
258261
if err != nil {
259262
return nil, fmt.Errorf("failed to build management endpoint: %w", err)
260263
}
261264
cacheParams := &CacheParams{
265+
ClusterNamespacedName: clusterNamespacedName,
262266
PrismManagementEndpoint: endpoint,
263267
}
264268

pkg/webhook/preflight/nutanix/credentials_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ func TestNewCredentialsCheck_SecretMissingKey(t *testing.T) {
172172
}
173173
cd := validCheckDependencies()
174174
cd.kclient = fake.NewClientBuilder().WithObjects(secret).Build()
175-
check := newCredentialsCheck(context.Background(), nil, cd)
175+
nclientFactory := func(_ prismgoclient.Credentials) (client, error) {
176+
return &clientWrapper{}, nil
177+
}
178+
check := newCredentialsCheck(context.Background(), nclientFactory, cd)
176179
result := check.Run(context.Background())
177180
assert.False(t, result.Allowed)
178181
assert.False(t, result.InternalError)

0 commit comments

Comments
 (0)