Skip to content

Commit

Permalink
feat: add kOps support (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
saumas authored Jun 9, 2021
1 parent 8f1df5e commit 0523aeb
Show file tree
Hide file tree
Showing 9 changed files with 677 additions and 33 deletions.
16 changes: 12 additions & 4 deletions internal/castai/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ type GKEParams struct {
ClusterName string `json:"clusterName"`
}

type KOPSParams struct {
CSP string `json:"cloud"`
Region string `json:"region"`
ClusterName string `json:"clusterName"`
StateStore string `json:"stateStore"`
}

type RegisterClusterRequest struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
EKS *EKSParams `json:"eks"`
GKE *GKEParams `json:"gke"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
EKS *EKSParams `json:"eks"`
GKE *GKEParams `json:"gke"`
KOPS *KOPSParams `json:"kops"`
}

type Cluster struct {
Expand Down
46 changes: 37 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Config struct {
CASTAI *CASTAI
EKS *EKS
GKE *GKE
KOPS *KOPS
}

type Log struct {
Expand Down Expand Up @@ -43,6 +44,13 @@ type GKE struct {
ClusterName string
}

type KOPS struct {
CSP string
Region string
ClusterName string
StateStore string
}

var cfg *Config

// Get configuration bound to environment variables.
Expand Down Expand Up @@ -71,6 +79,11 @@ func Get() Config {
_ = viper.BindEnv("gke.projectid", "GKE_PROJECT_ID")
_ = viper.BindEnv("gke.clustername", "GKE_CLUSTER_NAME")

_ = viper.BindEnv("kops.csp", "KOPS_CSP")
_ = viper.BindEnv("kops.region", "KOPS_REGION")
_ = viper.BindEnv("kops.clustername", "KOPS_CLUSTER_NAME")
_ = viper.BindEnv("kops.statestore", "KOPS_STATE_STORE")

cfg = &Config{}
if err := viper.Unmarshal(&cfg); err != nil {
panic(fmt.Errorf("parsing configuration: %v", err))
Expand All @@ -89,34 +102,49 @@ func Get() Config {

if cfg.CASTAI != nil {
if cfg.CASTAI.ClusterID == "" {
requiredDiscoveryDisabled("CASTAI_CLUSTER_ID")
requiredWhenDiscoveryDisabled("CASTAI_CLUSTER_ID")
}
if cfg.CASTAI.OrganizationID == "" {
requiredDiscoveryDisabled("CASTAI_ORGANIZATION_ID")
requiredWhenDiscoveryDisabled("CASTAI_ORGANIZATION_ID")
}
}

if cfg.EKS != nil {
if cfg.EKS.AccountID == "" {
requiredDiscoveryDisabled("EKS_ACCOUNT_ID")
requiredWhenDiscoveryDisabled("EKS_ACCOUNT_ID")
}
if cfg.EKS.Region == "" {
requiredDiscoveryDisabled("EKS_REGION")
requiredWhenDiscoveryDisabled("EKS_REGION")
}
if cfg.EKS.ClusterName == "" {
requiredDiscoveryDisabled("EKS_CLUSTER_NAME")
requiredWhenDiscoveryDisabled("EKS_CLUSTER_NAME")
}
}

if cfg.GKE != nil {
if cfg.GKE.Region == "" {
requiredDiscoveryDisabled("GKE_REGION")
requiredWhenDiscoveryDisabled("GKE_REGION")
}
if cfg.GKE.ProjectID == "" {
requiredDiscoveryDisabled("GKE_PROJECT_ID")
requiredWhenDiscoveryDisabled("GKE_PROJECT_ID")
}
if cfg.GKE.ClusterName == "" {
requiredDiscoveryDisabled("GKE_CLUSTER_NAME")
requiredWhenDiscoveryDisabled("GKE_CLUSTER_NAME")
}
}

if cfg.KOPS != nil {
if cfg.KOPS.CSP == "" {
requiredWhenDiscoveryDisabled("KOPS_CSP")
}
if cfg.KOPS.Region == "" {
requiredWhenDiscoveryDisabled("KOPS_REGION")
}
if cfg.KOPS.ClusterName == "" {
requiredWhenDiscoveryDisabled("KOPS_CLUSTER_NAME")
}
if cfg.KOPS.StateStore == "" {
requiredWhenDiscoveryDisabled("KOPS_STATE_STORE")
}
}

Expand All @@ -132,6 +160,6 @@ func required(variable string) {
panic(fmt.Errorf("env variable %s is required", variable))
}

func requiredDiscoveryDisabled(variable string) {
func requiredWhenDiscoveryDisabled(variable string) {
panic(fmt.Errorf("env variable %s is required when discovery is disabled", variable))
}
6 changes: 3 additions & 3 deletions internal/services/providers/gke/gke.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
const (
Name = "gke"

labelPreemptible = "cloud.google.com/gke-preemptible"
LabelPreemptible = "cloud.google.com/gke-preemptible"
)

func New(_ context.Context, log logrus.FieldLogger) (types.Provider, error) {
func New(log logrus.FieldLogger) (types.Provider, error) {
return &Provider{log: log}, nil
}

Expand Down Expand Up @@ -53,7 +53,7 @@ func (p *Provider) IsSpot(_ context.Context, node *corev1.Node) (bool, error) {
return true, nil
}

if val, ok := node.Labels[labelPreemptible]; ok && val == "true" {
if val, ok := node.Labels[LabelPreemptible]; ok && val == "true" {
return true, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/providers/gke/gke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestProvider_IsSpot(t *testing.T) {
},
{
name: "gke spot node",
node: &v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{labelPreemptible: "true"}}},
node: &v1.Node{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{LabelPreemptible: "true"}}},
expected: true,
},
{
Expand Down
Loading

0 comments on commit 0523aeb

Please sign in to comment.