From 8304a543902bc7029cea3dd132ff3fd26fbc8886 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Thu, 23 Jan 2020 06:40:54 +0100 Subject: [PATCH] feat: retrieve capability information from cluster (#70) Fixes #69 --- go.mod | 2 +- go.sum | 2 ++ pkg/hal/cli/capability/create.go | 59 ++++++++++++++++++++++++++++---- pkg/k8s/client.go | 19 ++++++---- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index f63f95a..5759a0e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 6ea77ec..f588c6b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/hal/cli/capability/create.go b/pkg/hal/cli/capability/create.go index 55f389d..85d9b95 100644 --- a/pkg/hal/cli/capability/create.go +++ b/pkg/hal/cli/capability/create.go @@ -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" @@ -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 @@ -111,8 +118,12 @@ 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 { @@ -120,8 +131,13 @@ func (o *createOptions) isValidCategory() bool { } 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 { @@ -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 { @@ -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{} diff --git a/pkg/k8s/client.go b/pkg/k8s/client.go index aa51f13..66e2088 100644 --- a/pkg/k8s/client.go +++ b/pkg/k8s/client.go @@ -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" @@ -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 @@ -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