-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adds a validation hook #498
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,33 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package variables | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
) | ||
|
||
func UnmarshalRuntimeVariable[T any](runtimeVariable *runtimehooksv1.Variable, obj *T) error { | ||
err := json.Unmarshal(runtimeVariable.Value.Raw, obj) | ||
if err != nil { | ||
return fmt.Errorf("error unmarshalling variable: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
//nolint:gocritic // no need for named results | ||
func GetRuntimhookVariableByName( | ||
name string, | ||
variables []runtimehooksv1.Variable, | ||
) (*runtimehooksv1.Variable, int) { | ||
faiq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for i, runtimevar := range variables { | ||
if runtimevar.Name == name { | ||
return &runtimevar, i | ||
} | ||
} | ||
return nil, -1 | ||
} |
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
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,24 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package validation | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/validation/helm" | ||
) | ||
|
||
type Handlers struct{} | ||
|
||
func New() *Handlers { | ||
return &Handlers{} | ||
} | ||
|
||
func (h *Handlers) AllHandlers(mgr manager.Manager) []handlers.Named { | ||
validationHandler := helm.New(mgr.GetClient()) | ||
return []handlers.Named{ | ||
validationHandler, | ||
} | ||
} |
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,87 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package helm | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
|
||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/variables" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" | ||
) | ||
|
||
type HelmRegistryValidator struct { | ||
client ctrlclient.Client | ||
variableName string | ||
} | ||
|
||
func New( | ||
c ctrlclient.Client, | ||
) *HelmRegistryValidator { | ||
return &HelmRegistryValidator{ | ||
client: c, | ||
variableName: clusterconfig.MetaVariableName, | ||
} | ||
} | ||
|
||
func (h *HelmRegistryValidator) Name() string { | ||
return "HelmRegistryValidator" | ||
} | ||
|
||
func (h *HelmRegistryValidator) ValidateTopology( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we please add a unit test for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im not sure about this particular function, but we can split it out to be more testable |
||
ctx context.Context, | ||
req *runtimehooksv1.ValidateTopologyRequest, | ||
res *runtimehooksv1.ValidateTopologyResponse, | ||
) { | ||
log := ctrl.LoggerFrom(ctx) | ||
clusterVar, ind := variables.GetRuntimhookVariableByName(h.variableName, req.Variables) | ||
if ind == -1 { | ||
log.V(5).Info(fmt.Sprintf("did not find variable %s in %v", h.variableName, req.Variables)) | ||
return | ||
} | ||
var cluster v1alpha1.ClusterConfig | ||
if err := variables.UnmarshalRuntimeVariable[v1alpha1.ClusterConfig](clusterVar, &cluster); err != nil { | ||
failString := fmt.Sprintf("failed to unmarshal variable %v to clusterConfig", clusterVar) | ||
log.Error(err, failString) | ||
res.SetStatus(runtimehooksv1.ResponseStatusFailure) | ||
res.SetMessage(failString) | ||
return | ||
} | ||
helmChartRepo := cluster.Spec.Addons.HelmChartRepository | ||
cl := &http.Client{ | ||
Transport: &http.Transport{ | ||
//nolint:gosec // this is done because customers can occasionally have self signed | ||
// or no certificates to OCI registries | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
} | ||
resp, err := cl.Get(fmt.Sprintf("%s/v2", *helmChartRepo)) | ||
if err != nil { | ||
failString := fmt.Sprintf("failed to ping provided helm registry %s", *helmChartRepo) | ||
log.Error(err, failString) | ||
res.SetStatus(runtimehooksv1.ResponseStatusFailure) | ||
res.SetMessage(failString) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusUnauthorized { | ||
res.SetStatus(runtimehooksv1.ResponseStatusSuccess) | ||
return | ||
} | ||
failString := fmt.Sprintf( | ||
"failed to get 401 or 200 response from hitting registry: %s got status: %d", | ||
*helmChartRepo, | ||
resp.StatusCode, | ||
) | ||
log.Error(err, failString) | ||
res.SetStatus(runtimehooksv1.ResponseStatusFailure) | ||
res.SetMessage(failString) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.