Skip to content

Commit

Permalink
feat: retrieve capability information from cluster (#70)
Browse files Browse the repository at this point in the history
Fixes #69
  • Loading branch information
metacosm authored and cmoulliard committed Jan 23, 2020
1 parent 434238d commit 8304a54
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/spf13/pflag v1.0.5
golang.org/x/crypto v0.0.0-20191107222254-f4817d981bb6
gopkg.in/AlecAivazis/survey.v1 v1.8.4
halkyon.io/api v1.0.0-rc.2
halkyon.io/api v1.0.0-rc.3
k8s.io/api v0.0.0-20190831074750-7364b6bdad65
k8s.io/apimachinery v0.0.0-20190831074630-461753078381
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
halkyon.io/api v1.0.0-rc.2 h1:O13sEG1aZW0jtm6jEfEeJ0ZPgSkLhIQZsr+iQOO7c2E=
halkyon.io/api v1.0.0-rc.2/go.mod h1:bCOZh54+rJ4SQad0L0OMO0B+Jf7J3OtKp7SPmdLowGE=
halkyon.io/api v1.0.0-rc.3 h1:evcR0y3QEXwLdYY9z1PKBZIXgsuxu//M81fEVGwLjGU=
halkyon.io/api v1.0.0-rc.3/go.mod h1:bCOZh54+rJ4SQad0L0OMO0B+Jf7J3OtKp7SPmdLowGE=
k8s.io/api v0.0.0-20190516230258-a675ac48af67 h1:BKg03K4me3EdM340RB08XB3MRlJ57KY+0f5PfI6wPZY=
k8s.io/api v0.0.0-20190516230258-a675ac48af67/go.mod h1:iuAfoD4hCxJ8Onx9kaTIt30j7jUFS00AXQi6QMi99vA=
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d h1:Jmdtdt1ZnoGfWWIIik61Z7nKYgO3J+swQJtPYsP9wHA=
Expand Down
59 changes: 53 additions & 6 deletions pkg/hal/cli/capability/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package capability
import (
"fmt"
"github.com/spf13/cobra"
v1beta12 "halkyon.io/api/capability-info/v1beta1"
"halkyon.io/api/capability/v1beta1"
halkyon "halkyon.io/api/v1beta1"
"halkyon.io/hal/pkg/cmdutil"
Expand All @@ -12,9 +13,15 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ktemplates "k8s.io/kubectl/pkg/util/templates"
"sort"
"strings"
)

type typeRegistry map[string][]string
type categoryRegisty map[string]typeRegistry

var categories = <-getCapabilityInfos()

type createOptions struct {
category string
subCategory string
Expand Down Expand Up @@ -111,17 +118,26 @@ func (p parameterInfo) AsValidatable() validation.Validatable {
}

func (o *createOptions) getCategories() []string {
// todo: implement operator querying of available capabilities
return []string{"database"}
result := make([]string, 0, len(categories))
for k := range categories {
result = append(result, k)
}
sort.Strings(result)
return result
}

func (o *createOptions) isValidCategory() bool {
return validation.IsValid(o.category, o.getCategories())
}

func (o *createOptions) getTypesFor(category string) []string {
// todo: implement operator querying for available types for given category
return []string{"postgres"}
types := categories[category]
result := make([]string, 0, len(types))
for k := range types {
result = append(result, k)
}
sort.Strings(result)
return result
}

func (o *createOptions) isValidTypeGivenCategory() bool {
Expand All @@ -133,8 +149,7 @@ func (o *createOptions) isValidTypeFor(category string) bool {
}

func (o *createOptions) getVersionsFor(category, subCategory string) []string {
// todo: implement operator querying
return []string{"11", "10", "9.6", "9.5", "9.4"}
return categories[category][subCategory]
}

func (o *createOptions) isValidVersionFor(category, subCategory string) bool {
Expand Down Expand Up @@ -200,6 +215,38 @@ func (o *createOptions) addValueFor(prop parameterInfo) {
}
}

func getCapabilityInfos() chan categoryRegisty {
r := make(chan categoryRegisty)

go func() {
list, err := k8s.GetClient().HalkyonCapabilityInfoClient.CapabilityInfos().List(v1.ListOptions{})
if err != nil {
panic(err)
}

capInfos := make(categoryRegisty, 11)
for _, item := range list.Items {
category := item.Spec.Category
types, ok := capInfos[category]
if !ok {
types = make(typeRegistry, 7)
capInfos[category] = types
}

_, ok = types[item.Spec.Type]
if !ok {
types[item.Spec.Type] = strings.Split(item.Spec.Versions, v1beta12.CapabilityInfoVersionSeparator)
} else {
panic(fmt.Errorf("a type named %s is already registered for category %s", item.Spec.Type, category))
}
}

r <- capInfos
}()

return r
}

func NewCmdCreate(parent string) *cobra.Command {
c := k8s.GetClient()
o := &createOptions{}
Expand Down
19 changes: 12 additions & 7 deletions pkg/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"github.com/pkg/errors"
capInfo "halkyon.io/api/capability-info/clientset/versioned/typed/capability-info/v1beta1"
capability "halkyon.io/api/capability/clientset/versioned/typed/capability/v1beta1"
component "halkyon.io/api/component/clientset/versioned/typed/component/v1beta1"
"halkyon.io/api/component/v1beta1"
Expand Down Expand Up @@ -32,13 +33,14 @@ const (
)

type Client struct {
KubeClient kubernetes.Interface
HalkyonComponentClient *component.HalkyonV1beta1Client
HalkyonLinkClient *link.HalkyonV1beta1Client
HalkyonCapabilityClient *capability.HalkyonV1beta1Client
HalkyonRuntimeClient *hruntime.HalkyonV1beta1Client
KubeConfig clientcmd.ClientConfig
Namespace string
KubeClient kubernetes.Interface
HalkyonComponentClient *component.HalkyonV1beta1Client
HalkyonLinkClient *link.HalkyonV1beta1Client
HalkyonCapabilityClient *capability.HalkyonV1beta1Client
HalkyonCapabilityInfoClient *capInfo.HalkyonV1beta1Client
HalkyonRuntimeClient *hruntime.HalkyonV1beta1Client
KubeConfig clientcmd.ClientConfig
Namespace string
}

var client *Client
Expand Down Expand Up @@ -70,6 +72,9 @@ func GetClient() *Client {
client.HalkyonRuntimeClient, err = hruntime.NewForConfig(config)
io2.LogErrorAndExit(err, "error creating halkyon runtime client")

client.HalkyonCapabilityInfoClient, err = capInfo.NewForConfig(config)
io2.LogErrorAndExit(err, "error creating halkyon capability info client")

namespace, _, err := client.KubeConfig.Namespace()
io2.LogErrorAndExit(err, "error retrieving namespace")
client.Namespace = namespace
Expand Down

0 comments on commit 8304a54

Please sign in to comment.