|
| 1 | +// Copyright 2023 D2iQ, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package helm |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "crypto/tls" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + |
| 12 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 13 | + ctrl "sigs.k8s.io/controller-runtime" |
| 14 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + |
| 16 | + "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 17 | + "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/variables" |
| 18 | + "github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig" |
| 19 | +) |
| 20 | + |
| 21 | +type HelmRegistryValidator struct { |
| 22 | + client ctrlclient.Client |
| 23 | + variableName string |
| 24 | +} |
| 25 | + |
| 26 | +func New( |
| 27 | + c ctrlclient.Client, |
| 28 | +) *HelmRegistryValidator { |
| 29 | + return &HelmRegistryValidator{ |
| 30 | + client: c, |
| 31 | + variableName: clusterconfig.MetaVariableName, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (h *HelmRegistryValidator) Name() string { |
| 36 | + return "HelmRegistryValidator" |
| 37 | +} |
| 38 | + |
| 39 | +func (h *HelmRegistryValidator) ValidateTopology( |
| 40 | + ctx context.Context, |
| 41 | + req *runtimehooksv1.ValidateTopologyRequest, |
| 42 | + res *runtimehooksv1.ValidateTopologyResponse, |
| 43 | +) { |
| 44 | + log := ctrl.LoggerFrom(ctx) |
| 45 | + clusterVar, ind := variables.GetRuntimhookVariableByName(h.variableName, req.Variables) |
| 46 | + if ind == -1 { |
| 47 | + log.V(5).Info(fmt.Sprintf("did not find variable %s in %v", h.variableName, req.Variables)) |
| 48 | + return |
| 49 | + } |
| 50 | + var cluster v1alpha1.ClusterConfig |
| 51 | + if err := variables.UnmarshalRuntimeVariable[v1alpha1.ClusterConfig](clusterVar, &cluster); err != nil { |
| 52 | + failString := fmt.Sprintf("failed to unmarshal variable %v to clusterConfig", clusterVar) |
| 53 | + log.Error(err, failString) |
| 54 | + res.SetStatus(runtimehooksv1.ResponseStatusFailure) |
| 55 | + res.SetMessage(failString) |
| 56 | + return |
| 57 | + } |
| 58 | + helmChartRepo := cluster.Spec.Addons.HelmChartRepository |
| 59 | + cl := &http.Client{ |
| 60 | + Transport: &http.Transport{ |
| 61 | + //nolint:gosec // this is done because customers can occasionally have self signed |
| 62 | + // or no certificates to OCI registries |
| 63 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 64 | + }, |
| 65 | + } |
| 66 | + resp, err := cl.Get(fmt.Sprintf("%s/v2", *helmChartRepo)) |
| 67 | + if err != nil { |
| 68 | + failString := fmt.Sprintf("failed to ping provided helm registry %s", *helmChartRepo) |
| 69 | + log.Error(err, failString) |
| 70 | + res.SetStatus(runtimehooksv1.ResponseStatusFailure) |
| 71 | + res.SetMessage(failString) |
| 72 | + return |
| 73 | + } |
| 74 | + defer resp.Body.Close() |
| 75 | + if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusUnauthorized { |
| 76 | + res.SetStatus(runtimehooksv1.ResponseStatusSuccess) |
| 77 | + return |
| 78 | + } |
| 79 | + failString := fmt.Sprintf( |
| 80 | + "failed to get 401 or 200 response from hitting registry: %s got status: %d", |
| 81 | + *helmChartRepo, |
| 82 | + resp.StatusCode, |
| 83 | + ) |
| 84 | + log.Error(err, failString) |
| 85 | + res.SetStatus(runtimehooksv1.ResponseStatusFailure) |
| 86 | + res.SetMessage(failString) |
| 87 | +} |
0 commit comments