Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit 5a25271

Browse files
committed
pkg/platform/aks: implement PostApplyHook
This commit implements newly introduced platform.PostApplyHook for AKS clusters, to address issue, where components depending on the storage gets installed when default storage class has not been yet created by the AKS controller, as this causes components to get stuck, which makes cluster provisioning to fail. The implemented hook lists available storage classes on the cluster and returns when class with default storage class annotation is found. Usually default storage class appears on the cluster within 5 minutes after cluster creation, so 10 minutes timeout seems like a sane default for this operation. Closes #855 Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
1 parent 13a0041 commit 5a25271

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

pkg/platform/aks/aks.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717
package aks
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"os"
2223
"path/filepath"
2324
"text/template"
25+
"time"
2426

2527
"github.com/hashicorp/hcl/v2"
2628
"github.com/hashicorp/hcl/v2/gohcl"
2729
"github.com/mitchellh/go-homedir"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
"k8s.io/apimachinery/pkg/util/wait"
32+
v1 "k8s.io/client-go/kubernetes/typed/storage/v1"
2833

34+
"github.com/kinvolk/lokomotive/pkg/k8sutil"
2935
"github.com/kinvolk/lokomotive/pkg/platform"
3036
"github.com/kinvolk/lokomotive/pkg/terraform"
3137
)
@@ -309,6 +315,48 @@ func (c *config) Initialize(ex *terraform.Executor) error {
309315
return createTerraformConfigFile(c, terraformRootDir)
310316
}
311317

318+
const (
319+
retryInterval = 5 * time.Second
320+
timeout = 10 * time.Minute
321+
)
322+
323+
// PostApplyHook implements platform.PostApplyHook interface and defines hooks
324+
// which should be executed after AKS cluster is created.
325+
func (c *config) PostApplyHook(kubeconfig []byte) error {
326+
client, err := k8sutil.NewClientset(kubeconfig)
327+
if err != nil {
328+
return fmt.Errorf("creating clientset from kubeconfig: %w", err)
329+
}
330+
331+
return waitForDefaultStorageClass(client.StorageV1().StorageClasses())
332+
}
333+
334+
// waitForDefaultStorageClass wait until default storage class appears on a given cluster.
335+
// If it doesn't within defined time range, error is returned.
336+
func waitForDefaultStorageClass(sci v1.StorageClassInterface) error {
337+
// AKS still uses annotation with .beta.
338+
defaultStorageClassAnnotation := "storageclass.beta.kubernetes.io/is-default-class"
339+
340+
if err := wait.PollImmediate(retryInterval, timeout, func() (done bool, err error) {
341+
scs, err := sci.List(context.TODO(), metav1.ListOptions{})
342+
if err != nil {
343+
return false, fmt.Errorf("getting storage classes %w", err)
344+
}
345+
346+
for _, sc := range scs.Items {
347+
if v, ok := sc.ObjectMeta.Annotations[defaultStorageClassAnnotation]; ok && v == "true" {
348+
return true, nil
349+
}
350+
}
351+
352+
return false, nil
353+
}); err != nil {
354+
return fmt.Errorf("waiting for the default storage class to be configured: %w", err)
355+
}
356+
357+
return nil
358+
}
359+
312360
// createTerraformConfigFiles create Terraform config files in given directory.
313361
func createTerraformConfigFile(cfg *config, terraformRootDir string) error {
314362
t := template.Must(template.New("t").Parse(terraformConfigTmpl))

0 commit comments

Comments
 (0)