Skip to content

Commit 2fc00d9

Browse files
committed
Enhance kubernetes audit support
1 parent f9769b9 commit 2fc00d9

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

exoscale/resource_exoscale_sks_cluster.go

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ import (
1717
)
1818

1919
const (
20-
defaultSKSClusterCNI = "calico"
21-
defaultSKSClusterServiceLevel = "pro"
22-
defaultSKSClusterAuditInitBackoff = "10s"
20+
defaultSKSClusterCNI = "calico"
21+
defaultSKSClusterServiceLevel = "pro"
22+
defaultSKSClusterAuditInitBackoff = "10s"
2323

2424
sksClusterAddonExoscaleCCM = "exoscale-cloud-controller"
2525
sksClusterAddonExoscaleCSI = "exoscale-container-storage-interface"
2626
sksClusterAddonMS = "metrics-server"
2727

2828
resSKSClusterAttrAddons = "addons"
2929
resSKSClusterAttrAggregationLayerCA = "aggregation_ca"
30-
resSKSClusterAttrAuditBearerToken = "audit_bearer_token"
31-
resSKSClusterAttrAuditEnabled = "audit_enabled"
32-
resSKSClusterAttrAuditEndpoint = "audit_endpoint"
33-
resSKSClusterAttrAuditInitBackoff = "audit_initial_backoff"
30+
resSKSClusterAttrAuditBearerToken = "audit_bearer_token"
31+
resSKSClusterAttrAuditEnabled = "audit_enabled"
32+
resSKSClusterAttrAuditEndpoint = "audit_endpoint"
33+
resSKSClusterAttrAuditInitBackoff = "audit_initial_backoff"
3434
resSKSClusterAttrAutoUpgrade = "auto_upgrade"
3535
resSKSClusterAttrCNI = "cni"
3636
resSKSClusterAttrControlPlaneCA = "control_plane_ca"
@@ -82,31 +82,27 @@ func resourceSKSCluster() *schema.Resource {
8282
Description: "The CA certificate (in PEM format) for TLS communications between the control plane and the aggregation layer (e.g. `metrics-server`).",
8383
},
8484
"audit": {
85-
Type: schema.TypeList,
86-
MaxItems: 1,
87-
Optional: true,
88-
Computed: true,
85+
Type: schema.TypeList,
86+
MaxItems: 1,
87+
Optional: true,
8988
Description: "Parameters for Kubernetes Audit configuration (may only be enabled at creation time)",
9089
Elem: &schema.Resource{
91-
Schema: map[string]*schema.Schema {
92-
resSKSClusterAttrAuditEnabled: {
93-
Type: schema.TypeBool,
94-
Required: false,
90+
Schema: map[string]*schema.Schema{
91+
resSKSClusterAttrAuditEnabled: {
92+
Type: schema.TypeBool,
9593
Description: "Whether to run the APIServer with the configured Kubernetes Audit",
9694
},
9795
resSKSClusterAttrAuditEndpoint: {
98-
Type: schema.TypeString,
99-
Required: true,
96+
Type: schema.TypeString,
97+
Required: true,
10098
Description: "The Endpoint URL for the Webserver responsible of processing Audit events",
10199
},
102100
resSKSClusterAttrAuditInitBackoff: {
103-
Type: schema.TypeString,
104-
Required: false,
101+
Type: schema.TypeString,
105102
Description: "The Initial Backoff to wait before sending data to the remote server (default '10s')",
106103
},
107104
resSKSClusterAttrAuditBearerToken: {
108-
Type: schema.TypeString,
109-
Required: false,
105+
Type: schema.TypeString,
110106
Description: "The optional bearer token to include in the request header",
111107
},
112108
},
@@ -393,16 +389,19 @@ func resourceSKSClusterCreate(ctx context.Context, d *schema.ResourceData, meta
393389
}
394390
createReq.Version = version
395391

396-
if v, ok := d.GetOk(resSKSClusterAttrAudit(resSKSClusterAttrAuditEndpoint)); ok {
397-
createReq.Audit = &v3.SKSAuditCreate{
398-
Endpoint: v.(v3.SKSAuditEndpoint),
399-
}
392+
auditEnabled := d.Get(resSKSClusterAttrAuditEnabled).(bool)
393+
if auditEnabled {
394+
if v, ok := d.GetOk(resSKSClusterAttrAudit(resSKSClusterAttrAuditEndpoint)); ok {
395+
createReq.Audit = &v3.SKSAuditCreate{
396+
Endpoint: v.(v3.SKSAuditEndpoint),
397+
}
400398

401-
if v, ok := d.GetOk(resSKSClusterAttrAudit(resSKSClusterAttrAuditBearerToken)); ok {
402-
createReq.Audit.BearerToken = v.(v3.SKSAuditBearerToken)
403-
}
404-
if v, ok := d.GetOk(resSKSClusterAttrAudit(resSKSClusterAttrAuditInitBackoff)); ok {
405-
createReq.Audit.InitialBackoff = v.(v3.SKSAuditInitialBackoff)
399+
if v, ok := d.GetOk(resSKSClusterAttrAudit(resSKSClusterAttrAuditBearerToken)); ok {
400+
createReq.Audit.BearerToken = v.(v3.SKSAuditBearerToken)
401+
}
402+
if v, ok := d.GetOk(resSKSClusterAttrAudit(resSKSClusterAttrAuditInitBackoff)); ok {
403+
createReq.Audit.InitialBackoff = v.(v3.SKSAuditInitialBackoff)
404+
}
406405
}
407406
}
408407

@@ -625,8 +624,27 @@ func resourceSKSClusterUpdate(ctx context.Context, d *schema.ResourceData, meta
625624
}
626625
}
627626

628-
if d.HasChange(resSKSClusterAttrAuditEndpoint) {
629-
// TODO
627+
if d.HasChange(resSKSClusterAttrAuditEndpoint) || d.HasChange(resSKSClusterAttrAuditEnabled) ||
628+
d.HasChange(resSKSClusterAttrAuditBearerToken) || d.HasChange(resSKSClusterAttrAuditInitBackoff) {
629+
enableAudit := d.Get(resSKSClusterAttrAuditEnabled).(bool)
630+
updateReq.Audit = &v3.SKSAuditUpdate{
631+
Enabled: &enableAudit,
632+
Endpoint: v3.SKSAuditEndpoint(d.Get(resSKSClusterAttrAuditEndpoint).(string)),
633+
}
634+
635+
if enableAudit && updateReq.Audit.Endpoint == "" {
636+
return diag.Errorf("cannot enable audit without setting an endpoint")
637+
}
638+
639+
if v, ok := d.GetOk(resSKSClusterAttrAuditBearerToken); ok {
640+
updateReq.Audit.BearerToken = v.(v3.SKSAuditBearerToken)
641+
}
642+
643+
if v, ok := d.GetOk(resSKSClusterAttrAuditInitBackoff); ok {
644+
updateReq.Audit.InitialBackoff = v.(v3.SKSAuditInitialBackoff)
645+
}
646+
647+
updated = true
630648
}
631649

632650
if updated {

0 commit comments

Comments
 (0)