Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
Signed-off-by: Benedict Schlueter <[email protected]>
  • Loading branch information
benschlueter committed Oct 14, 2024
1 parent 1c5a6f0 commit a8b53ce
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (k *installer) connectToEtcd(_ context.Context, creds *config.EtcdCredentia
if err != nil {
return err
}
if err := k.client.ConnectToStore(creds, []string{net.JoinHostPort(host, "2379")}); err != nil {
if err := k.client.ConnectToStoreExternal(creds, []string{net.JoinHostPort(host, "2379")}); err != nil {
k.logger.With(zap.Error(err)).Error("failed to connect to store")
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/terminate/terminate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewTerminate(logger *zap.Logger, creds *config.EtcdCredentials) (Terminate,
if err != nil {
return nil, err
}
if err := client.ConnectToStore(creds, []string{net.JoinHostPort(host, "2379")}); err != nil {
if err := client.ConnectToStoreExternal(creds, []string{net.JoinHostPort(host, "2379")}); err != nil {
return nil, err
}
return &terminate{kubeClient: client, logger: logger}, nil
Expand Down
76 changes: 74 additions & 2 deletions internal/k8sapi/k8sapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
package k8sapi

import (
"context"
"errors"
"net"
"os"
"strings"
"sync"
"time"

"github.com/benschlueter/delegatio/internal/config"
"github.com/benschlueter/delegatio/internal/store"
Expand Down Expand Up @@ -63,8 +67,8 @@ func NewClient(logger *zap.Logger) (kubeClient *Client, err error) {
return
}

// ConnectToStore connects to the etcd store.
func (k *Client) ConnectToStore(creds *config.EtcdCredentials, endpoints []string) error {
// ConnectToStoreExternal connects to the etcd store.
func (k *Client) ConnectToStoreExternal(creds *config.EtcdCredentials, endpoints []string) error {
if k.SharedStore != nil {
k.logger.Info("client is already connected to store, reconnecting")
}
Expand Down Expand Up @@ -130,5 +134,73 @@ func GetKubeConfigPath() (string, error) {
return val, nil
}

func (k *Client) GetStore() (store.Store, error) {
if k.SharedStore == nil {
if err := k.ConnectToStoreInternal(); err != nil {
k.logger.Info("client is not connected to etcd")
return nil, err
}
}
return k.SharedStore, nil
}

// GetStore returns a store backed by kube etcd. Its only supposed to used within a kubernetes pod.
// Before calling this the installer needs to populate a config map with the respective credentials.
// Furthermore a serviceaccount must be set up for the namespace and it needs to be attached to the
// running pod.
func (k *Client) ConnectToStoreInternal() error {
var err error
var ns string
if _, err := os.Stat(config.NameSpaceFilePath); errors.Is(err, os.ErrNotExist) {
// ns is not ready when container spawns
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ns, err = waitForNamespaceMount(ctx)
if err != nil {
k.logger.Error("failed to get namespace after timeout", zap.Error(err))
return err
}
} else {
// out of cluster mode currently assumes 'ssh' namespace
if content, err := os.ReadFile(config.NameSpaceFilePath); err == nil {
ns = strings.TrimSpace(string(content))
} else {
return err
}
}
k.logger.Info("namespace", zap.String("namespace", ns))
configData, err := k.GetConfigMapData(context.Background(), ns, "etcd-credentials")
if err != nil {
return err
}
// logger.Info("config", zap.Any("configData", configData))
etcdStore, err := store.NewEtcdStore([]string{net.JoinHostPort(configData["advertiseAddr"], "2379")}, k.logger, []byte(configData["caCert"]), []byte(configData["cert"]), []byte(configData["key"]))
if err != nil {
return err
}
k.SharedStore = etcdStore
}

Check failure on line 182 in internal/k8sapi/k8sapi.go

View workflow job for this annotation

GitHub Actions / golangci

missing return (typecheck)

Check failure on line 182 in internal/k8sapi/k8sapi.go

View workflow job for this annotation

GitHub Actions / test

missing return

// waitForNamespaceMount waits for the namespace file to be mounted and filled.
func waitForNamespaceMount(ctx context.Context) (string, error) {
t := time.NewTicker(100 * time.Millisecond)
defer t.Stop()
for {
select {
case <-ctx.Done():
return "", ctx.Err()
case <-t.C:
data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil && !errors.Is(err, os.ErrNotExist) {
return "", err
}
ns := strings.TrimSpace(string(data))
if len(ns) != 0 {
return ns, nil
}
}
}
}

// ErrNotConnected is returned when the client is not connected to etcd.
var ErrNotConnected = errors.New("client is not connected to etcd")

0 comments on commit a8b53ce

Please sign in to comment.