Skip to content

Commit

Permalink
fromConfigItem in exec connection
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Nov 6, 2024
1 parent 32b4a8b commit 4ce4113
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
36 changes: 36 additions & 0 deletions connection/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import (

"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

textTemplate "text/template"
)

// +kubebuilder:object:generate=true
type ExecConnections struct {
FromConfigItem string `yaml:"fromConfigItem,omitempty" json:"fromConfigItem,omitempty" template:"true"`

Kubernetes *KubernetesConnection `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty"`
AWS *AWSConnection `yaml:"aws,omitempty" json:"aws,omitempty"`
GCP *GCPConnection `yaml:"gcp,omitempty" json:"gcp,omitempty"`
Expand Down Expand Up @@ -68,6 +74,36 @@ func SetupConnection(ctx context.Context, connections ExecConnections, cmd *osEx
return nil
}

if connections.FromConfigItem != "" {
var configItem models.ConfigItem
if err := ctx.DB().Where("id = ?", connections.FromConfigItem).First(&configItem).Error; err != nil {
return nil, fmt.Errorf("failed to get config (%s): %w", connections.FromConfigItem, err)
}

if lo.FromPtr(configItem.Type) != "MissionControl::ScrapeConfig" {
return nil, fmt.Errorf("provided config item (type=%s) cannot be used to populate connection. must be a MissionControl::ScrapeConfig", lo.FromPtr(configItem.Type))
}

if config, err := configItem.ConfigJSONStringMap(); err != nil {
return nil, fmt.Errorf("provided config item (id=%s) is malformed", configItem.ID)
} else {
if kubernetesScrapers, found, err := unstructured.NestedSlice(config, "spec", "kubernetes"); err != nil {
return nil, err
} else if found {
for _, kscraper := range kubernetesScrapers {
if kubeconfig, found, err := unstructured.NestedMap(kscraper.(map[string]any), "kubeconfig"); err != nil {
return nil, err
} else if found {
connections.Kubernetes = &KubernetesConnection{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(kubeconfig, &connections.Kubernetes.KubeConfig); err != nil {
return nil, err
}
}
}
}
}
}

if connections.Kubernetes != nil {
configPath, err := saveConfig(kubernetesConfigTemplate, connections.Kubernetes)
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions connection/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

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

func (t KubernetesConnection) ToModel() models.Connection {
Expand All @@ -20,8 +20,6 @@ func (t KubernetesConnection) ToModel() models.Connection {
}
}

// 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)
Expand All @@ -34,5 +32,11 @@ func (t *KubernetesConnection) Populate(ctx ConnectionContext) error {
t.KubeConfig.ValueStatic = connection.Certificate
}

if v, err := ctx.GetEnvValueFromCache(t.KubeConfig, ctx.GetNamespace()); err != nil {
return err
} else {
t.KubeConfig.ValueStatic = v
}

return nil
}
6 changes: 1 addition & 5 deletions 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 4ce4113

Please sign in to comment.