Skip to content

Commit

Permalink
Load mappings late.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio committed Nov 19, 2024
1 parent 343517b commit dbe796b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
11 changes: 8 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,19 @@ func getInitCommand() *cobra.Command {
return fmt.Errorf("error creating container: %w", err)
}

// TODO hack, the kube api is not always ready need to figure out a better condition
time.Sleep(time.Second * 7)

k8sClient, err := k8s.New(log, filepath.Join(pwd, constants.KubeconfigHostPath))
if err != nil {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

if err := k8sClient.WaitForKubernetesAPIHealthy(ctx, 5*time.Minute); err != nil {
return fmt.Errorf("error waiting for kubernetes api to be healthy: %w", err)
}

if err := k8sClient.LoadMappingsFromKubernetes(); err != nil {
return fmt.Errorf("error loading dynamic mappings from kubernetes: %w", err)
}

if err := k8sClient.FetchAndWaitForDeployments(ctx, k8s.DeploymentDetails{
Label: "kubernetes.io/name",
Value: "CoreDNS",
Expand Down
50 changes: 38 additions & 12 deletions internal/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,29 @@ func New(logger *logger.Logger, kubeConfig string) (*Client, error) {
return nil, fmt.Errorf("error creating dynamic client: %w", err)
}

discovery, err := discovery.NewDiscoveryClientForConfig(config)
return &Client{
clientSet: clientset,
dynamic: dynamic,
config: config,
SecretName: "colony-api",
logger: logger,
}, nil
}

func (c *Client) LoadMappingsFromKubernetes() error {
discovery, err := discovery.NewDiscoveryClientForConfig(c.config)
if err != nil {
return nil, fmt.Errorf("error creating discovery client: %w", err)
return fmt.Errorf("error creating discovery client: %w", err)
}

groupResources, err := restmapper.GetAPIGroupResources(discovery)
if err != nil {
return nil, fmt.Errorf("error getting API group resources: %w", err)
return fmt.Errorf("error getting API group resources: %w", err)
}

restmapper := restmapper.NewDiscoveryRESTMapper(groupResources)
c.restmapper = restmapper.NewDiscoveryRESTMapper(groupResources)

return &Client{
clientSet: clientset,
dynamic: dynamic,
restmapper: restmapper,
config: config,
SecretName: "colony-api",
logger: logger,
}, nil
return nil
}

func (c *Client) CreateAPIKeySecret(ctx context.Context, apiKey string) error {
Expand Down Expand Up @@ -422,3 +425,26 @@ func BuildConfigMap(name, script string) (*corev1.ConfigMap, error) {

return cm, nil
}

// WaitForKubernetesAPIHealthy waits for the Kubernetes API to be healthy
// by checking the server version every 5 seconds or until the timeout is reached.
func (c *Client) WaitForKubernetesAPIHealthy(ctx context.Context, timeout time.Duration) error {
err := wait.PollUntilContextTimeout(ctx, 5*time.Second, timeout, true, func(ctx context.Context) (bool, error) {

Check failure on line 432 in internal/k8s/k8s.go

View workflow job for this annotation

GitHub Actions / run-tests

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
_, err := c.clientSet.Discovery().ServerVersion()
if err != nil {
if isNetworkingError(err) {
c.logger.Warn("connection to kube-apiserver error, retrying: %s", err)
return false, nil
}

return false, fmt.Errorf("error getting server version: %w", err)
}

return true, nil
})
if err != nil {
return fmt.Errorf("error waiting for Kubernetes API to be healthy: %w", err)
}

return nil
}

0 comments on commit dbe796b

Please sign in to comment.