Skip to content

Commit

Permalink
Add support for creating component from service catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
surajnarwade committed Apr 24, 2018
1 parent 4455fb3 commit 87a49d2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
6 changes: 5 additions & 1 deletion cmd/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"

"github.com/redhat-developer/odo/pkg/catalog"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -30,7 +31,10 @@ odo catalog list
default:
fmt.Println("The following components can be deployed:")
for _, component := range catalogList {
fmt.Printf("- %v\n", component)
for _, compo := range component {
fmt.Printf("- %v\n", compo)
}

}
}
},
Expand Down
5 changes: 4 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ A full list of component types that can be deployed is available using: 'odo com
//We don't have to check it anymore, Args check made sure that args has at least one item
// and no more than two
componentType := args[0]
exists, err := catalog.Exists(client, componentType)
exists, err, typo := catalog.Exists(client, componentType)
checkError(err, "")
if !exists {
fmt.Printf("Invalid component type: %v\nRun 'odo catalog list' to see a list of supported components\n", componentType)
Expand Down Expand Up @@ -116,6 +116,9 @@ A full list of component types that can be deployed is available using: 'odo com
checkError(err, "")
fmt.Printf("Component '%s' was created.\n", componentName)
fmt.Printf("To push source code to the component run 'odo push'\n")
} else if len(typo) != 0 && typo == "template" {
err = component.CreateFromTemplate(client)
checkError(err, "")
} else {
// we want to use and save absolute path for component
dir, err := filepath.Abs("./")
Expand Down
56 changes: 40 additions & 16 deletions pkg/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@ import (
)

// List lists all the available component types
func List(client *occlient.Client) ([]string, error) {
func List(client *occlient.Client) (finalList map[string][]string, err error) {
var catalogList []string
imageStreams, err := getDefaultBuilderImages(client)
if err != nil {
return nil, errors.Wrap(err, "unable to get image streams")
}
catalogList = append(catalogList, imageStreams...)

finalList = make(map[string][]string)
finalList["normal"] = catalogList
// TODO: uncomment when component create supports template creation
//clusterServiceClasses, err := client.GetClusterServiceClassExternalNames()
//if err != nil {
// return nil, errors.Wrap(err, "unable to get cluster service classes")
//}
//catalogList = append(catalogList, clusterServiceClasses...)
clusterServiceClasses, err := client.GetClusterServiceClassExternalNames()
if err != nil {
return nil, errors.Wrap(err, "unable to get cluster service classes")
}
var clusterSvcClass []string
for _, csclass := range clusterServiceClasses {
if !strings.Contains(csclass, "apb") {
clusterSvcClass = append(clusterSvcClass, csclass)
}
}
finalList["template"] = clusterSvcClass
//catalogList = append(catalogList, clusterSvcClass...)

return catalogList, nil
return finalList, nil
}

// Search searches for the component
Expand All @@ -37,27 +45,33 @@ func Search(client *occlient.Client, name string) ([]string, error) {

// do a partial search in all the components
for _, component := range componentList {
if strings.Contains(component, name) {
result = append(result, component)
for _, compo := range component {
if strings.Contains(compo, name) {
result = append(result, compo)
}
}

}

return result, nil
}

// Exists returns true if the given component type is valid, false if not
func Exists(client *occlient.Client, componentType string) (bool, error) {
func Exists(client *occlient.Client, componentType string) (bool, error, string) {
catalogList, err := List(client)
if err != nil {
return false, errors.Wrapf(err, "unable to list catalog")
return false, errors.Wrapf(err, "unable to list catalog"), ""
}

for _, supported := range catalogList {
if componentType == supported {
return true, nil
for typo, compo := range catalogList {
for _, supported := range compo {
if componentType == supported {
return true, nil, typo
}
}

}
return false, nil
return false, nil, ""
}

// getDefaultBuilderImages returns the default builder images available in the
Expand Down Expand Up @@ -86,3 +100,13 @@ outer:
log.Debugf("Found builder images: %v", builderImages)
return builderImages, nil
}

//func getCatalogList() {
// app := &svcat.App{
// SDK: &servicecatalog.SDK{
// ServiceCatalogClient: cl,
// },
// CurrentNamespace: ns,
// }
//
//}
9 changes: 9 additions & 0 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ func CreateFromDir(client *occlient.Client, name string, ctype string, dir strin

}

func CreateFromTemplate(client *occlient.Client) error {
err := client.CreateServiceInstance()
if err != nil {
return err

}
return nil
}

// Delete whole component
func Delete(client *occlient.Client, name string, applicationName string, projectName string) (string, error) {

Expand Down
9 changes: 9 additions & 0 deletions pkg/occlient/occlient.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,12 +873,21 @@ func (c *Client) GetBuildConfig(name string, project string) (*buildv1.BuildConf
// currently available cluster service classes
func (c *Client) GetClusterServiceClasses() ([]scv1beta1.ClusterServiceClass, error) {
classList, err := c.serviceCatalogClient.ClusterServiceClasses().List(metav1.ListOptions{})
//_, err := c.serviceCatalogClient.ServiceInstances("myproject").Create(&scv1beta1.ServiceInstance{metav1.TypeMeta{Kind: "ServiceInstance", APIVersion: "servicecatalog.k8s.io/v1beta1"}, metav1.ObjectMeta{Finalizers: []string{"kubernetes-incubator/service-catalog"}, Name: "jenkins", Namespace: "myproject"}, scv1beta1.ServiceInstanceSpec{UserInfo: &scv1beta1.UserInfo{Username: "developer"}, PlanReference: scv1beta1.PlanReference{ClusterServiceClassExternalName: "jenkins-ephemeral"}}, scv1beta1.ServiceInstanceStatus{}})
if err != nil {
return nil, errors.Wrap(err, "unable to list cluster service classes")
}
return classList.Items, nil
}

func (c *Client) CreateServiceInstance() error {
_, err := c.serviceCatalogClient.ServiceInstances("myproject").Create(&scv1beta1.ServiceInstance{metav1.TypeMeta{Kind: "ServiceInstance", APIVersion: "servicecatalog.k8s.io/v1beta1"}, metav1.ObjectMeta{Finalizers: []string{"kubernetes-incubator/service-catalog"}, Name: "jenkins", Namespace: "myproject"}, scv1beta1.ServiceInstanceSpec{UserInfo: &scv1beta1.UserInfo{Username: "developer"}, PlanReference: scv1beta1.PlanReference{ClusterServiceClassExternalName: "jenkins-ephemeral"}}, scv1beta1.ServiceInstanceStatus{}})
if err != nil {
return errors.Wrap(err, "unable to create service instance")
}
return nil
}

// GetClusterServiceClassExternalNames returns the names of all the cluster service
// classes in the cluster
func (c *Client) GetClusterServiceClassExternalNames() ([]string, error) {
Expand Down

0 comments on commit 87a49d2

Please sign in to comment.