Skip to content

Commit

Permalink
Improve the wait-for-apiserver ready check
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
  • Loading branch information
mikkeloscar committed Dec 4, 2023
1 parent ddc8570 commit dec714a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
25 changes: 19 additions & 6 deletions provisioner/clusterpy.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func (p *clusterpyProvisioner) Provision(ctx context.Context, logger *log.Entry,
cluster,
caNodePoolProvisioner,
karpenterProvisioner,
p.tokenSource,
)

err = nodePoolGroups["masters"].provisionNodePoolGroup(ctx, values, updater, cluster, p.applyOnly)
Expand Down Expand Up @@ -709,14 +710,26 @@ func (p *clusterpyProvisioner) removeEBSVolumes(awsAdapter *awsAdapter, cluster

// waitForAPIServer waits a cluster API server to be ready. It's considered
// ready when it's reachable.
func waitForAPIServer(logger *log.Entry, server string, maxTimeout time.Duration) error {
func waitForAPIServer(logger *log.Entry, cluster *api.Cluster, maxTimeout time.Duration, tokenSource oauth2.TokenSource) error {
logger.Infof("Waiting for API Server to be reachable")
client := &http.Client{}
timeout := time.Now().UTC().Add(maxTimeout)

for time.Now().UTC().Before(timeout) {
resp, err := client.Get(server)
if err == nil && resp.StatusCode < http.StatusInternalServerError {
req, err := http.NewRequest(http.MethodGet, cluster.APIServerURL+"/readyz", nil)
if err != nil {
return err
}

token, err := tokenSource.Token()
if err != nil {
return err
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))

resp, err := client.Do(req)
if err == nil && resp.StatusCode == http.StatusOK {
return nil
}

Expand All @@ -725,7 +738,7 @@ func waitForAPIServer(logger *log.Entry, server string, maxTimeout time.Duration
time.Sleep(15 * time.Second)
}

return fmt.Errorf("'%s' was not ready after %s", server, maxTimeout.String())
return fmt.Errorf("%q was not ready after %s", cluster.APIServerURL, maxTimeout.String())
}

// setupAWSAdapter sets up the AWS Adapter used for communicating with AWS.
Expand Down Expand Up @@ -1136,7 +1149,7 @@ type nodePoolGroup struct {
ReadyFn func() error
}

func groupNodePools(logger *log.Entry, cluster *api.Cluster, caProvisioner *AWSNodePoolProvisioner, karProvisioner *KarpenterNodePoolProvisioner) map[string]*nodePoolGroup {
func groupNodePools(logger *log.Entry, cluster *api.Cluster, caProvisioner *AWSNodePoolProvisioner, karProvisioner *KarpenterNodePoolProvisioner, tokenSource oauth2.TokenSource) map[string]*nodePoolGroup {

var masters, workers, karpenterPools []*api.NodePool
for _, nodePool := range cluster.NodePools {
Expand All @@ -1157,7 +1170,7 @@ func groupNodePools(logger *log.Entry, cluster *api.Cluster, caProvisioner *AWSN
NodePools: masters,
Provisioner: caProvisioner,
ReadyFn: func() error {
return waitForAPIServer(logger, cluster.APIServerURL, 15*time.Minute)
return waitForAPIServer(logger, cluster, 15*time.Minute, tokenSource)
},
},
"workers": {
Expand Down
40 changes: 40 additions & 0 deletions provisioner/clusterpy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ package provisioner
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zalando-incubator/cluster-lifecycle-manager/api"
"github.com/zalando-incubator/cluster-lifecycle-manager/channel"
"github.com/zalando-incubator/cluster-lifecycle-manager/pkg/kubernetes"
"golang.org/x/oauth2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -477,3 +482,38 @@ second: 2
})
}
}

func TestWaitForAPIServer(t *testing.T) {
for _, tc := range []struct {
name string
responseCode int
}{
{
name: "test successful response from wait",
responseCode: 200,
},
{
name: "test unuccessful response during wait",
responseCode: 500,
},
} {
t.Run(tc.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tc.responseCode)
}))
defer ts.Close()

cluster := &api.Cluster{
APIServerURL: ts.URL,
}

tokenSource := oauth2.StaticTokenSource(&oauth2.Token{})
err := waitForAPIServer(log.WithField("cluster", "test"), cluster, 1*time.Millisecond, tokenSource)
if tc.responseCode == http.StatusOK {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}

0 comments on commit dec714a

Please sign in to comment.