Skip to content

Commit 5c8706a

Browse files
committed
add logging throughout ClusterResource
1 parent 2859ccd commit 5c8706a

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

terraform-provider-constellation/internal/provider/cluster_resource.go

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,78 +787,103 @@ func (r *ClusterResource) validateGCPNetworkConfig(ctx context.Context, data *Cl
787787
// apply applies changes to a cluster. It can be used for both creating and updating a cluster.
788788
// This implements the core part of the Create and Update methods.
789789
func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel, skipInitRPC, skipNodeUpgrade bool) diag.Diagnostics {
790+
r.logWithContext(ctx, "INFO", "Starting cluster apply", map[string]interface{}{
791+
"skipInitRPC": skipInitRPC,
792+
"skipNodeUpgrade": skipNodeUpgrade,
793+
})
794+
790795
diags := diag.Diagnostics{}
791796

792797
// Parse and convert values from the Terraform state
793798
// to formats the Constellation library can work with.
799+
r.logWithContext(ctx, "DEBUG", "Validating GCP network config")
794800
convertDiags := r.validateGCPNetworkConfig(ctx, data)
795801
diags.Append(convertDiags...)
796802
if diags.HasError() {
803+
r.logWithContext(ctx, "ERROR", "Failed to validate GCP network config", map[string]interface{}{"error": diags.Errors()})
797804
return diags
798805
}
799806

800807
csp := cloudprovider.FromString(data.CSP.ValueString())
808+
r.logWithContext(ctx, "DEBUG", "Parsed CSP", map[string]interface{}{"csp": csp.String()})
801809

802810
// parse attestation config
811+
r.logWithContext(ctx, "DEBUG", "Converting attestation config")
803812
att, convertDiags := r.convertAttestationConfig(ctx, *data)
804813
diags.Append(convertDiags...)
805814
if diags.HasError() {
815+
r.logWithContext(ctx, "ERROR", "Failed to convert attestation config", map[string]interface{}{"error": diags.Errors()})
806816
return diags
807817
}
808818

809819
// parse secrets (i.e. measurement salt, master secret, etc.)
820+
r.logWithContext(ctx, "DEBUG", "Converting secrets")
810821
secrets, convertDiags := r.convertSecrets(*data)
811822
diags.Append(convertDiags...)
812823
if diags.HasError() {
824+
r.logWithContext(ctx, "ERROR", "Failed to convert secrets", map[string]interface{}{"error": diags.Errors()})
813825
return diags
814826
}
815827

816828
// parse API server certificate SANs
829+
r.logWithContext(ctx, "DEBUG", "Getting API server cert SANs")
817830
apiServerCertSANs, convertDiags := r.getAPIServerCertSANs(ctx, data)
818831
diags.Append(convertDiags...)
819832
if diags.HasError() {
833+
r.logWithContext(ctx, "ERROR", "Failed to get API server cert SANs", map[string]interface{}{"error": diags.Errors()})
820834
return diags
821835
}
822836

823837
// parse network config
838+
r.logWithContext(ctx, "DEBUG", "Getting network config")
824839
networkCfg, getDiags := r.getNetworkConfig(ctx, data)
825840
diags.Append(getDiags...)
826841
if diags.HasError() {
842+
r.logWithContext(ctx, "ERROR", "Failed to get network config", map[string]interface{}{"error": diags.Errors()})
827843
return diags
828844
}
829845

830846
// parse Constellation microservice config
847+
r.logWithContext(ctx, "DEBUG", "Parsing microservice config")
831848
var microserviceCfg extraMicroservicesAttribute
832849
convertDiags = data.ExtraMicroservices.As(ctx, &microserviceCfg, basetypes.ObjectAsOptions{
833850
UnhandledNullAsEmpty: true, // we want to allow null values, as the CSIDriver field is optional
834851
})
835852
diags.Append(convertDiags...)
836853
if diags.HasError() {
854+
r.logWithContext(ctx, "ERROR", "Failed to parse microservice config", map[string]interface{}{"error": diags.Errors()})
837855
return diags
838856
}
839857

840858
// parse Constellation microservice version
859+
r.logWithContext(ctx, "DEBUG", "Getting microservice version")
841860
microserviceVersion, convertDiags := r.getMicroserviceVersion(data)
842861
diags.Append(convertDiags...)
843862
if diags.HasError() {
863+
r.logWithContext(ctx, "ERROR", "Failed to get microservice version", map[string]interface{}{"error": diags.Errors()})
844864
return diags
845865
}
846866

847867
// parse Kubernetes version
868+
r.logWithContext(ctx, "DEBUG", "Getting Kubernetes version")
848869
k8sVersion, getDiags := r.getK8sVersion(data)
849870
diags.Append(getDiags...)
850871
if diags.HasError() {
872+
r.logWithContext(ctx, "ERROR", "Failed to get Kubernetes version", map[string]interface{}{"error": diags.Errors()})
851873
return diags
852874
}
853875

854876
// parse OS image version
877+
r.logWithContext(ctx, "DEBUG", "Getting OS image version")
855878
image, imageSemver, convertDiags := r.getImageVersion(ctx, data)
856879
diags.Append(convertDiags...)
857880
if diags.HasError() {
881+
r.logWithContext(ctx, "ERROR", "Failed to get OS image version", map[string]interface{}{"error": diags.Errors()})
858882
return diags
859883
}
860884

861885
// parse license ID
886+
r.logWithContext(ctx, "DEBUG", "Parsing license ID")
862887
licenseID := data.LicenseID.ValueString()
863888
switch {
864889
case image.MarketplaceImage != nil && *image.MarketplaceImage:
@@ -874,6 +899,7 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
874899
}
875900

876901
// Parse in-cluster service account info.
902+
r.logWithContext(ctx, "DEBUG", "Parsing service account info")
877903
serviceAccPayload := constellation.ServiceAccountPayload{}
878904
var gcpConfig gcpAttribute
879905
var azureConfig azureAttribute
@@ -883,6 +909,7 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
883909
convertDiags = data.GCP.As(ctx, &gcpConfig, basetypes.ObjectAsOptions{})
884910
diags.Append(convertDiags...)
885911
if diags.HasError() {
912+
r.logWithContext(ctx, "ERROR", "Failed to parse GCP config", map[string]interface{}{"error": diags.Errors()})
886913
return diags
887914
}
888915

@@ -906,6 +933,7 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
906933
convertDiags = data.Azure.As(ctx, &azureConfig, basetypes.ObjectAsOptions{})
907934
diags.Append(convertDiags...)
908935
if diags.HasError() {
936+
r.logWithContext(ctx, "ERROR", "Failed to parse Azure config", map[string]interface{}{"error": diags.Errors()})
909937
return diags
910938
}
911939
serviceAccPayload.Azure = azureshared.ApplicationCredentials{
@@ -918,6 +946,7 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
918946
convertDiags = data.OpenStack.As(ctx, &openStackConfig, basetypes.ObjectAsOptions{})
919947
diags.Append(convertDiags...)
920948
if diags.HasError() {
949+
r.logWithContext(ctx, "ERROR", "Failed to parse OpenStack config", map[string]interface{}{"error": diags.Errors()})
921950
return diags
922951
}
923952
cloudsYAML, err := clouds.ReadCloudsYAML(file.NewHandler(afero.NewOsFs()), openStackConfig.CloudsYAMLPath)
@@ -942,8 +971,11 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
942971
}
943972

944973
}
974+
975+
r.logWithContext(ctx, "DEBUG", "Marshalling service account URI")
945976
serviceAccURI, err := constellation.MarshalServiceAccountURI(csp, serviceAccPayload)
946977
if err != nil {
978+
r.logWithContext(ctx, "ERROR", "Failed to marshal service account URI", map[string]interface{}{"error": err.Error()})
947979
diags.AddError("Marshalling service account URI", err.Error())
948980
return diags
949981
}
@@ -955,14 +987,17 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
955987
}
956988

957989
// setup clients
990+
r.logWithContext(ctx, "DEBUG", "Setting up clients")
958991
validator, err := choose.Validator(att.config, &tfContextLogger{ctx: ctx})
959992
if err != nil {
993+
r.logWithContext(ctx, "ERROR", "Failed to choose validator", map[string]interface{}{"error": err.Error()})
960994
diags.AddError("Choosing validator", err.Error())
961995
return diags
962996
}
963997
applier := r.newApplier(ctx, validator)
964998

965999
// Construct in-memory state file
1000+
r.logWithContext(ctx, "DEBUG", "Constructing in-memory state file")
9661001
stateFile := state.New().SetInfrastructure(state.Infrastructure{
9671002
UID: data.UID.ValueString(),
9681003
ClusterEndpoint: data.OutOfClusterEndpoint.ValueString(),
@@ -995,21 +1030,24 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
9951030
}
9961031

9971032
// Check license
1033+
r.logWithContext(ctx, "DEBUG", "Checking license")
9981034
quota, err := applier.CheckLicense(ctx, csp, !skipInitRPC, licenseID)
9991035
if err != nil {
1036+
r.logWithContext(ctx, "WARN", "Unable to contact license server", map[string]interface{}{"error": err.Error()})
10001037
diags.AddWarning("Unable to contact license server.", "Please keep your vCPU quota in mind.")
10011038
} else if licenseID == license.CommunityLicense {
1039+
r.logWithContext(ctx, "WARN", "Using community license")
10021040
diags.AddWarning("Using community license.", "For details, see https://docs.edgeless.systems/constellation/overview/license")
10031041
} else {
1004-
tflog.Info(ctx, fmt.Sprintf("Please keep your vCPU quota (%d) in mind.", quota))
1042+
r.logWithContext(ctx, "INFO", "License check completed", map[string]interface{}{"vCPU_quota": quota})
10051043
}
10061044

10071045
// Now, we perform the actual applying.
10081046

10091047
// Run init RPC
1010-
var initDiags diag.Diagnostics
10111048
if !skipInitRPC {
10121049
// run the init RPC and retrieve the post-init state
1050+
r.logWithContext(ctx, "INFO", "Running init RPC")
10131051
initRPCPayload := initRPCPayload{
10141052
csp: csp,
10151053
masterSecret: secrets.masterSecret,
@@ -1022,16 +1060,18 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
10221060
k8sVersion: k8sVersion,
10231061
inClusterEndpoint: inClusterEndpoint,
10241062
}
1025-
initDiags = r.runInitRPC(ctx, applier, initRPCPayload, data, validator, stateFile)
1063+
initDiags := r.runInitRPC(ctx, applier, initRPCPayload, data, validator, stateFile)
10261064
diags.Append(initDiags...)
10271065
if diags.HasError() {
1066+
r.logWithContext(ctx, "ERROR", "Init RPC failed", map[string]interface{}{"error": diags.Errors()})
10281067
return diags
10291068
}
10301069
}
10311070

10321071
// Here, we either have the post-init values from the actual init RPC
10331072
// or, if performing an upgrade and skipping the init RPC, we have the
10341073
// values from the Terraform state.
1074+
r.logWithContext(ctx, "DEBUG", "Setting cluster values")
10351075
stateFile.SetClusterValues(state.ClusterValues{
10361076
ClusterID: data.ClusterID.ValueString(),
10371077
OwnerID: data.OwnerID.ValueString(),
@@ -1040,25 +1080,32 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
10401080

10411081
// Kubeconfig is in the state by now. Either through the init RPC or through
10421082
// already being in the state.
1083+
r.logWithContext(ctx, "DEBUG", "Setting kubeconfig")
10431084
if err := applier.SetKubeConfig([]byte(data.KubeConfig.ValueString())); err != nil {
1085+
r.logWithContext(ctx, "ERROR", "Failed to set kubeconfig", map[string]interface{}{"error": err.Error()})
10441086
diags.AddError("Setting kubeconfig", err.Error())
10451087
return diags
10461088
}
10471089

10481090
// Apply attestation config
1091+
r.logWithContext(ctx, "DEBUG", "Applying attestation config")
10491092
if err := applier.ApplyJoinConfig(ctx, att.config, secrets.measurementSalt); err != nil {
1093+
r.logWithContext(ctx, "ERROR", "Failed to apply attestation config", map[string]interface{}{"error": err.Error()})
10501094
diags.AddError("Applying attestation config", err.Error())
10511095
return diags
10521096
}
10531097

10541098
// Extend API Server Certificate SANs
1099+
r.logWithContext(ctx, "DEBUG", "Extending API server certificate SANs")
10551100
if err := applier.ExtendClusterConfigCertSANs(ctx, data.OutOfClusterEndpoint.ValueString(),
10561101
"", apiServerCertSANs); err != nil {
1102+
r.logWithContext(ctx, "ERROR", "Failed to extend API server certificate SANs", map[string]interface{}{"error": err.Error()})
10571103
diags.AddError("Extending API server certificate SANs", err.Error())
10581104
return diags
10591105
}
10601106

10611107
// Apply Helm Charts
1108+
r.logWithContext(ctx, "INFO", "Applying Helm charts")
10621109
payload := applyHelmChartsPayload{
10631110
csp: cloudprovider.FromString(data.CSP.ValueString()),
10641111
attestationVariant: att.variant,
@@ -1079,38 +1126,47 @@ func (r *ClusterResource) apply(ctx context.Context, data *ClusterResourceModel,
10791126
helmDiags := r.applyHelmCharts(ctx, applier, payload, stateFile)
10801127
diags.Append(helmDiags...)
10811128
if diags.HasError() {
1129+
r.logWithContext(ctx, "ERROR", "Failed to apply Helm charts", map[string]interface{}{"error": diags.Errors()})
10821130
return diags
10831131
}
10841132

10851133
if !skipNodeUpgrade {
10861134
// Upgrade node image
1135+
r.logWithContext(ctx, "INFO", "Upgrading node image")
10871136
err = applier.UpgradeNodeImage(ctx,
10881137
imageSemver,
10891138
image.Reference,
10901139
false)
10911140
var upgradeImageErr *compatibility.InvalidUpgradeError
10921141
switch {
10931142
case errors.Is(err, kubecmd.ErrInProgress):
1143+
r.logWithContext(ctx, "WARN", "Skipping OS image upgrade: Another upgrade is already in progress")
10941144
diags.AddWarning("Skipping OS image upgrade", "Another upgrade is already in progress.")
10951145
case errors.As(err, &upgradeImageErr):
1146+
r.logWithContext(ctx, "WARN", "Ignoring invalid OS image upgrade", map[string]interface{}{"error": err.Error()})
10961147
diags.AddWarning("Ignoring invalid OS image upgrade", err.Error())
10971148
case err != nil:
1149+
r.logWithContext(ctx, "ERROR", "Failed to upgrade OS image", map[string]interface{}{"error": err.Error()})
10981150
diags.AddError("Upgrading OS image", err.Error())
10991151
return diags
11001152
}
11011153

11021154
// Upgrade Kubernetes components
1155+
r.logWithContext(ctx, "INFO", "Upgrading Kubernetes components")
11031156
err = applier.UpgradeKubernetesVersion(ctx, k8sVersion, false)
11041157
var upgradeK8sErr *compatibility.InvalidUpgradeError
11051158
switch {
11061159
case errors.As(err, &upgradeK8sErr):
1160+
r.logWithContext(ctx, "WARN", "Ignoring invalid Kubernetes components upgrade", map[string]interface{}{"error": err.Error()})
11071161
diags.AddWarning("Ignoring invalid Kubernetes components upgrade", err.Error())
11081162
case err != nil:
1163+
r.logWithContext(ctx, "ERROR", "Failed to upgrade Kubernetes components", map[string]interface{}{"error": err.Error()})
11091164
diags.AddError("Upgrading Kubernetes components", err.Error())
11101165
return diags
11111166
}
11121167
}
11131168

1169+
r.logWithContext(ctx, "INFO", "Cluster apply completed successfully")
11141170
return diags
11151171
}
11161172

0 commit comments

Comments
 (0)