Skip to content

Commit

Permalink
feat: kubernetes Connection object
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Nov 5, 2024
1 parent 19d4736 commit 32b4a8b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
13 changes: 6 additions & 7 deletions connection/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (

"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/types"

textTemplate "text/template"
)

// +kubebuilder:object:generate=true
type ExecConnections struct {
Kubernetes *types.EnvVar `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty"`
AWS *AWSConnection `yaml:"aws,omitempty" json:"aws,omitempty"`
GCP *GCPConnection `yaml:"gcp,omitempty" json:"gcp,omitempty"`
Azure *AzureConnection `yaml:"azure,omitempty" json:"azure,omitempty"`
Kubernetes *KubernetesConnection `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty"`
AWS *AWSConnection `yaml:"aws,omitempty" json:"aws,omitempty"`
GCP *GCPConnection `yaml:"gcp,omitempty" json:"gcp,omitempty"`
Azure *AzureConnection `yaml:"azure,omitempty" json:"azure,omitempty"`
}

func saveConfig(configTemplate *textTemplate.Template, view any) (string, error) {
Expand Down Expand Up @@ -59,7 +58,7 @@ aws_secret_access_key = {{.SecretKey.ValueStatic}}

gcloudConfigTemplate = textTemplate.Must(textTemplate.New("").Parse(`{{.Credentials}}`))

kubernetesConfigTemplate = textTemplate.Must(textTemplate.New("").Parse(`{{.ValueStatic}}`))
kubernetesConfigTemplate = textTemplate.Must(textTemplate.New("").Parse(`{{.KubeConfig.ValueStatic}}`))
}

// SetupCConnections creates the necessary credential files and injects env vars
Expand All @@ -72,7 +71,7 @@ func SetupConnection(ctx context.Context, connections ExecConnections, cmd *osEx
if connections.Kubernetes != nil {
configPath, err := saveConfig(kubernetesConfigTemplate, connections.Kubernetes)
if err != nil {
return nil, fmt.Errorf("failed to store AWS credentials: %w", err)
return nil, fmt.Errorf("failed to store kubernetes credentials: %w", err)
}

cleaner = func() error {
Expand Down
38 changes: 38 additions & 0 deletions connection/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package connection

import (
"fmt"

"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
)

// +kubebuilder:object:generate=true
type KubernetesConnection struct {
ConnectionName string `json:"connection,omitempty"`
KubeConfig *types.EnvVar `json:"kubeconfig,omitempty"`
}

func (t KubernetesConnection) ToModel() models.Connection {
return models.Connection{
Type: models.ConnectionTypeKubernetes,
Certificate: t.KubeConfig.ValueStatic,
}
}

// Populate populates KubernetesConnection with credentials.
// If a connection name is specified, it'll be used to populate the certificate.
func (t *KubernetesConnection) Populate(ctx ConnectionContext) error {
if t.ConnectionName != "" {
connection, err := ctx.HydrateConnectionByURL(t.ConnectionName)
if err != nil {
return err
} else if connection == nil {
return fmt.Errorf("connection[%s] not found", t.ConnectionName)
}

t.KubeConfig.ValueStatic = connection.Certificate
}

return nil
}
22 changes: 21 additions & 1 deletion connection/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 32b4a8b

Please sign in to comment.