|
| 1 | +package spec |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v5" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + |
| 14 | + "go.goms.io/aks/AKSFlexNode/pkg/auth" |
| 15 | + "go.goms.io/aks/AKSFlexNode/pkg/config" |
| 16 | + "go.goms.io/aks/AKSFlexNode/pkg/utils" |
| 17 | +) |
| 18 | + |
| 19 | +// ManagedClusterClient is the subset of the Azure SDK managed clusters client we need. |
| 20 | +// It exists to allow lightweight mocking in unit tests. |
| 21 | +type ManagedClusterClient interface { |
| 22 | + Get(ctx context.Context, resourceGroupName, resourceName string, options *armcontainerservice.ManagedClustersClientGetOptions) (armcontainerservice.ManagedClustersClientGetResponse, error) |
| 23 | +} |
| 24 | + |
| 25 | +// ManagedClusterSpecEnricher enriches the collected spec snapshot based on the managed cluster response. |
| 26 | +// It enables adding more spec signals in the future without changing the collector control-flow. |
| 27 | +type ManagedClusterSpecEnricher func(spec *ManagedClusterSpec, resp armcontainerservice.ManagedClustersClientGetResponse) error |
| 28 | + |
| 29 | +// ManagedClusterSpecCollector collects spec from the target AKS managed cluster |
| 30 | +// and persists it to a local file for later checking. |
| 31 | +type ManagedClusterSpecCollector struct { |
| 32 | + cfg *config.Config |
| 33 | + logger *logrus.Logger |
| 34 | + |
| 35 | + authProvider *auth.AuthProvider |
| 36 | + client ManagedClusterClient |
| 37 | + |
| 38 | + outputPath string |
| 39 | + |
| 40 | + enrichers []ManagedClusterSpecEnricher |
| 41 | +} |
| 42 | + |
| 43 | +// NewManagedClusterSpecCollector creates a collector that writes to the default spec path. |
| 44 | +// The Azure client is created lazily on the first Collect call. |
| 45 | +func NewManagedClusterSpecCollector(cfg *config.Config, logger *logrus.Logger) *ManagedClusterSpecCollector { |
| 46 | + c := &ManagedClusterSpecCollector{ |
| 47 | + cfg: cfg, |
| 48 | + logger: logger, |
| 49 | + authProvider: auth.NewAuthProvider(), |
| 50 | + outputPath: GetManagedClusterSpecFilePath(), |
| 51 | + } |
| 52 | + // Keep KubernetesVersion, fqdn required for now; more enrichers can be added over time. |
| 53 | + c.enrichers = []ManagedClusterSpecEnricher{enrichKubernetesVersionRequired, enrichFQDNRequired} |
| 54 | + return c |
| 55 | +} |
| 56 | + |
| 57 | +// NewManagedClusterSpecCollectorWithClient allows injecting a ManagedClusterClient and output path (primarily for tests). |
| 58 | +func NewManagedClusterSpecCollectorWithClient(cfg *config.Config, logger *logrus.Logger, client ManagedClusterClient, outputPath string) *ManagedClusterSpecCollector { |
| 59 | + c := NewManagedClusterSpecCollector(cfg, logger) |
| 60 | + c.client = client |
| 61 | + if outputPath != "" { |
| 62 | + c.outputPath = outputPath |
| 63 | + } |
| 64 | + return c |
| 65 | +} |
| 66 | + |
| 67 | +// AddEnricher appends a spec enricher. |
| 68 | +func (c *ManagedClusterSpecCollector) AddEnricher(enricher ManagedClusterSpecEnricher) { |
| 69 | + if c == nil || enricher == nil { |
| 70 | + return |
| 71 | + } |
| 72 | + c.enrichers = append(c.enrichers, enricher) |
| 73 | +} |
| 74 | + |
| 75 | +// Collect queries the Azure managed cluster resource to retrieve a spec snapshot. |
| 76 | +// It writes a JSON payload to the configured output path and returns the collected spec. |
| 77 | +func (c *ManagedClusterSpecCollector) Collect(ctx context.Context) (*ManagedClusterSpec, error) { |
| 78 | + if c == nil { |
| 79 | + return nil, fmt.Errorf("collector is nil") |
| 80 | + } |
| 81 | + if c.cfg == nil { |
| 82 | + return nil, fmt.Errorf("config is nil") |
| 83 | + } |
| 84 | + if c.logger == nil { |
| 85 | + c.logger = logrus.New() |
| 86 | + } |
| 87 | + |
| 88 | + clusterName := c.cfg.GetTargetClusterName() |
| 89 | + clusterRG := c.cfg.GetTargetClusterResourceGroup() |
| 90 | + if clusterName == "" || clusterRG == "" { |
| 91 | + return nil, fmt.Errorf("target cluster name/resourceGroup missing (name=%q, resourceGroup=%q)", clusterName, clusterRG) |
| 92 | + } |
| 93 | + |
| 94 | + if c.client == nil { |
| 95 | + subscriptionID := c.cfg.GetTargetClusterSubscriptionID() |
| 96 | + if subscriptionID == "" { |
| 97 | + subscriptionID = c.cfg.GetSubscriptionID() |
| 98 | + } |
| 99 | + if subscriptionID == "" { |
| 100 | + return nil, fmt.Errorf("subscription ID missing") |
| 101 | + } |
| 102 | + |
| 103 | + cred, err := c.authProvider.UserCredential(c.cfg) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("failed to get credential: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + mcClient, err := armcontainerservice.NewManagedClustersClient(subscriptionID, cred, nil) |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("failed to create managed clusters client: %w", err) |
| 111 | + } |
| 112 | + c.client = mcClient |
| 113 | + } |
| 114 | + |
| 115 | + c.logger.Infof("Collecting managed cluster spec for %s/%s", clusterRG, clusterName) |
| 116 | + resp, err := c.client.Get(ctx, clusterRG, clusterName, nil) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("failed to get AKS managed cluster via SDK: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + spec := &ManagedClusterSpec{ |
| 122 | + SchemaVersion: ManagedClusterSpecSchemaVersion, |
| 123 | + ClusterResourceID: c.cfg.GetTargetClusterID(), |
| 124 | + ClusterName: clusterName, |
| 125 | + ResourceGroup: clusterRG, |
| 126 | + CollectedAt: time.Now().UTC(), |
| 127 | + } |
| 128 | + |
| 129 | + for _, enricher := range c.enrichers { |
| 130 | + if enricher == nil { |
| 131 | + continue |
| 132 | + } |
| 133 | + if err := enricher(spec, resp); err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + data, err := json.MarshalIndent(spec, "", " ") |
| 139 | + if err != nil { |
| 140 | + return nil, fmt.Errorf("failed to marshal spec JSON: %w", err) |
| 141 | + } |
| 142 | + data = append(data, '\n') |
| 143 | + |
| 144 | + if err := os.MkdirAll(filepath.Dir(c.outputPath), 0o750); err != nil { |
| 145 | + return nil, fmt.Errorf("failed to create spec output directory: %w", err) |
| 146 | + } |
| 147 | + if err := utils.WriteFileAtomicSystem(c.outputPath, data, 0o644); err != nil { |
| 148 | + return nil, fmt.Errorf("failed to write managed cluster spec file: %w", err) |
| 149 | + } |
| 150 | + |
| 151 | + return spec, nil |
| 152 | +} |
| 153 | + |
| 154 | +func enrichKubernetesVersionRequired(spec *ManagedClusterSpec, resp armcontainerservice.ManagedClustersClientGetResponse) error { |
| 155 | + if spec == nil { |
| 156 | + return fmt.Errorf("spec is nil") |
| 157 | + } |
| 158 | + // set kubernetesVersion |
| 159 | + if resp.Properties == nil || resp.Properties.KubernetesVersion == nil || *resp.Properties.KubernetesVersion == "" { |
| 160 | + return fmt.Errorf("managed cluster kubernetesVersion is empty") |
| 161 | + } |
| 162 | + spec.KubernetesVersion = *resp.Properties.KubernetesVersion |
| 163 | + |
| 164 | + // set currentKubernetesVersion |
| 165 | + if resp.Properties == nil || resp.Properties.CurrentKubernetesVersion == nil || *resp.Properties.CurrentKubernetesVersion == "" { |
| 166 | + return fmt.Errorf("managed cluster currentKubernetesVersion is empty") |
| 167 | + } |
| 168 | + spec.CurrentKubernetesVersion = *resp.Properties.CurrentKubernetesVersion |
| 169 | + return nil |
| 170 | +} |
| 171 | + |
| 172 | +func enrichFQDNRequired(spec *ManagedClusterSpec, resp armcontainerservice.ManagedClustersClientGetResponse) error { |
| 173 | + if spec == nil { |
| 174 | + return fmt.Errorf("spec is nil") |
| 175 | + } |
| 176 | + if resp.Properties == nil || resp.Properties.Fqdn == nil || *resp.Properties.Fqdn == "" { |
| 177 | + return fmt.Errorf("managed cluster FQDN is empty") |
| 178 | + } |
| 179 | + spec.Fqdn = *resp.Properties.Fqdn |
| 180 | + return nil |
| 181 | +} |
0 commit comments