Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
[FEATURE] global, cluster and project catalogs as code (#39 #44)
Browse files Browse the repository at this point in the history
* Add Descriptor for Rancher with Catalog client

* Add Descriptor for Cluster with Catalog client

* Add Descriptor for Project with Catalog client

* Add catalog samples
  • Loading branch information
wtschreiter authored Jun 26, 2019
1 parent 99ca9a8 commit e0c1da7
Show file tree
Hide file tree
Showing 143 changed files with 2,449 additions and 121 deletions.
3 changes: 2 additions & 1 deletion cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/bitgrip/cattlectl/cmd/utils"
"github.com/bitgrip/cattlectl/internal/pkg/config"
"github.com/bitgrip/cattlectl/internal/pkg/ctl"
"github.com/bitgrip/cattlectl/internal/pkg/rancher/project"
"github.com/bitgrip/cattlectl/internal/pkg/rancher/cluster/project"
"github.com/bitgrip/cattlectl/internal/pkg/template"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -48,6 +48,7 @@ var (
newProjectParser = project.NewProjectParser
)

// BaseCommand is accessor to the package base command
func BaseCommand(config config.Config, init func()) *cobra.Command {
rootConfig = config
initCommand = init
Expand Down
2 changes: 1 addition & 1 deletion cmd/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/bitgrip/cattlectl/cmd/utils"
"github.com/bitgrip/cattlectl/internal/pkg/config"
"github.com/bitgrip/cattlectl/internal/pkg/ctl"
"github.com/bitgrip/cattlectl/internal/pkg/rancher/project"
"github.com/bitgrip/cattlectl/internal/pkg/rancher/cluster/project"
"github.com/bitgrip/cattlectl/internal/pkg/template"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down
38 changes: 38 additions & 0 deletions docs/cluster_descriptor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Cluster Descriptor data model
=============================

ClusterDescriptor Structur:
---------------------------

### Toplevel ClusterDescriptor

| Field | Description |
|-----------------|-----------------------------------------------------------------------|
| __api_version__ | The __\<major\>.\<minor\>__ version used for this descriptor. |
| __kind__ | The kind of descriptor in this file (`Project`) |
| __metadata__ | Metainformation about this descriptor e.g.: name and cluster_name |
| __catalogs__ | List of namespaces to be part of this project |

### metadata

* In the descriptor only `name` should be set.
* All other fields are read from configuration or rancher

| Field | Description |
|------------------|------------------------------------------------------------------------------------------|
| __name__ | The name of the cluster **Optional or placed from cattleclt configuration** |
| __id__ | Rancher internal ID of this project (**read from rancher**) |
| __rancher_url__ | The URL to reach the rancher (**placed from cattleclt configuration**) |
| __access_key__ | The access key to access rancher with (**placed from cattleclt configuration**) |
| __secret_key__ | The secret key to access rancher with (**placed from cattleclt configuration**) |
| __token_key__ | The token key to access rancher with (**placed from cattleclt configuration**) |

#### catalogs

| Field | Description |
|--------------|-----------------------------|
| __name__ | The name of the catalog |
| __url__ | The URL of the catalog |
| __branch__ | The branch of the catalog |
| __username__ | The username of the catalog |
| __password__ | The password of the catalog |
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ Finding more informations
-------------------------

* [Descriptor templates](descriptor_templates.md)
* [RancherDescriptor data model](rancher_descriptor.md)
* [ClusterDescriptor data model](cluster_descriptor.md)
* [ProjectDescriptor data model](project_descriptor.md)
* [cattlectl CLI documentation](cattlectl.md)
Empty file added docs/rancher_descriptor.md
Empty file.
165 changes: 144 additions & 21 deletions internal/pkg/ctl/cattle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,30 @@ import (
"fmt"

"github.com/bitgrip/cattlectl/internal/pkg/config"
rancher "github.com/bitgrip/cattlectl/internal/pkg/rancher"
rancher_client "github.com/bitgrip/cattlectl/internal/pkg/rancher/client"
"github.com/bitgrip/cattlectl/internal/pkg/rancher/project"
projectModel "github.com/bitgrip/cattlectl/internal/pkg/rancher/project/model"
cluster "github.com/bitgrip/cattlectl/internal/pkg/rancher/cluster"
clusterModel "github.com/bitgrip/cattlectl/internal/pkg/rancher/cluster/model"
"github.com/bitgrip/cattlectl/internal/pkg/rancher/cluster/project"
projectModel "github.com/bitgrip/cattlectl/internal/pkg/rancher/cluster/project/model"
rancherModel "github.com/bitgrip/cattlectl/internal/pkg/rancher/model"
yaml "gopkg.in/yaml.v2"
)

// Supported descriptor expected in the field 'kind'
const (
ProjectKind = "Project"
JobKind = "Job"
CronJobKind = "CronJob"
DeploymentKind = "Deployment"
DaemonSetKind = "DaemonSet"
StatefulSetKind = "StatefulSet"
)

var (
newRancherClient = rancher_client.NewRancherClient

newRancherConverger = rancher.NewRancherConverger
newClusterConverger = cluster.NewClusterConverger
newProjectConverger = project.NewProjectConverger
newJobConverger = project.NewJobConverger
newCronJobConverger = project.NewCronJobConverger
newDeploymentConverger = project.NewDeploymentConverger
newDaemonSetConverger = project.NewDaemonSetConverger
newStatefulSetConverger = project.NewStatefulSetConverger

newRancherParser = rancher.NewRancherParser
newClusterParser = cluster.NewClusterParser
newProjectParser = project.NewProjectParser
newJobParser = project.NewJobParser
newCronJobParser = project.NewCronJobParser
Expand All @@ -62,47 +60,63 @@ func ApplyDescriptor(file string, data []byte, values map[string]interface{}, co
return fmt.Errorf("Unsupported api version %s", apiVersion)
}
switch kind {
case ProjectKind:
case rancherModel.RancherKind:
descriptor := rancherModel.Rancher{}
if err := newRancherParser(file, values).Parse(data, &descriptor); err != nil {
return err
}
if err := ApplyRancher(descriptor, config); err != nil {
return err
}
case rancherModel.ClusterKind:
descriptor := clusterModel.Cluster{}
if err := newClusterParser(file, values).Parse(data, &descriptor); err != nil {
return err
}
if err := ApplyCluster(descriptor, config); err != nil {
return err
}
case rancherModel.ProjectKind:
project := projectModel.Project{}
if err := newProjectParser(file, values).Parse(data, &project); err != nil {
return err
}
if err := ApplyProject(project, config); err != nil {
return err
}
case JobKind:
case rancherModel.JobKind:
jobDescriptor := projectModel.JobDescriptor{}
if err := newJobParser(file, values).Parse(data, &jobDescriptor); err != nil {
return err
}
if err := ApplyJob(jobDescriptor, config); err != nil {
return err
}
case CronJobKind:
case rancherModel.CronJobKind:
cronJobDescriptor := projectModel.CronJobDescriptor{}
if err := newCronJobParser(file, values).Parse(data, &cronJobDescriptor); err != nil {
return err
}
if err := ApplyCronJob(cronJobDescriptor, config); err != nil {
return err
}
case DeploymentKind:
case rancherModel.DeploymentKind:
deploymentDescriptor := projectModel.DeploymentDescriptor{}
if err := newDeploymentParser(file, values).Parse(data, &deploymentDescriptor); err != nil {
return err
}
if err := ApplyDeployment(deploymentDescriptor, config); err != nil {
return err
}
case DaemonSetKind:
case rancherModel.DaemonSetKind:
daemonSetDescriptor := projectModel.DaemonSetDescriptor{}
if err := newDaemonSetParser(file, values).Parse(data, &daemonSetDescriptor); err != nil {
return err
}
if err := ApplyDaemonSet(daemonSetDescriptor, config); err != nil {
return err
}
case StatefulSetKind:
case rancherModel.StatefulSetKind:
statefulSetDescriptor := projectModel.StatefulSetDescriptor{}
if err := newStatefulSetParser(file, values).Parse(data, &statefulSetDescriptor); err != nil {
return err
Expand Down Expand Up @@ -194,6 +208,32 @@ func ApplyProject(project projectModel.Project, config config.Config) error {
return converger.Converge()
}

// ApplyCluster the the CTL perform a apply action to a cluster descriptor
func ApplyCluster(cluster clusterModel.Cluster, config config.Config) error {
rancherClient, err := fillClusterMetadata(&cluster.Metadata, config)
if err != nil {
return err
}
converger, err := newClusterConverger(cluster, rancherClient)
if err != nil {
return err
}
return converger.Converge()
}

// ApplyRancher the the CTL perform a apply action to a cluster descriptor
func ApplyRancher(rancher rancherModel.Rancher, config config.Config) error {
rancherConfig, err := fillRancherMetadata(&rancher.Metadata, config)
if err != nil {
return err
}
converger, err := newRancherConverger(rancher, rancherConfig)
if err != nil {
return err
}
return converger.Converge()
}

// GetAPIVersionAndKind reads API version and kind from the data
func GetAPIVersionAndKind(data []byte) (string, string, error) {
structure := make(map[string]interface{})
Expand Down Expand Up @@ -227,24 +267,51 @@ func ParseAndPrintDescriptor(file string, data []byte, values map[string]interfa
}
var descriptor interface{}
switch kind {
case ProjectKind:
case rancherModel.RancherKind:
descriptor = rancherModel.Rancher{}
if err = newRancherParser(file, values).Parse(data, &descriptor); err != nil {
return err
}
case rancherModel.ClusterKind:
descriptor = clusterModel.Cluster{}
if err = newRancherParser(file, values).Parse(data, &descriptor); err != nil {
return err
}
case rancherModel.ProjectKind:
project := projectModel.Project{}
if err = newProjectParser(file, values).Parse(data, &project); err != nil {
return err
}
descriptor = project
case JobKind:
case rancherModel.JobKind:
jobDescriptor := projectModel.JobDescriptor{}
if err = newJobParser(file, values).Parse(data, &jobDescriptor); err != nil {
return err
}
descriptor = jobDescriptor
case CronJobKind:
case rancherModel.CronJobKind:
cronJobDescriptor := projectModel.CronJobDescriptor{}
if err = newCronJobParser(file, values).Parse(data, &cronJobDescriptor); err != nil {
return err
}
descriptor = cronJobDescriptor
case rancherModel.DeploymentKind:
descriptor = projectModel.Deployment{}
if err = newDeploymentParser(file, values).Parse(data, &descriptor); err != nil {
return err
}
case rancherModel.StatefulSetKind:
descriptor = projectModel.StatefulSet{}
if err = newStatefulSetParser(file, values).Parse(data, &descriptor); err != nil {
return err
}
case rancherModel.DaemonSetKind:
descriptor = projectModel.DaemonSet{}
if err = newDaemonSetParser(file, values).Parse(data, &descriptor); err != nil {
return err
}
default:
return fmt.Errorf("Unknown descriptor %s", kind)
}
out, err := yaml.Marshal(descriptor)
if err != nil {
Expand Down Expand Up @@ -333,3 +400,59 @@ func fillProjectMetadata(metadata *projectModel.ProjectMetadata, config config.C
}
return rancherClient, clusterClient, nil
}

func fillClusterMetadata(metadata *clusterModel.ClusterMetadata, config config.Config) (rancher_client.RancherClient, error) {
if config.RancherURL() != "" {
metadata.RancherURL = config.RancherURL()
}
if config.AccessKey() != "" {
metadata.AccessKey = config.AccessKey()
}
if config.SecretKey() != "" {
metadata.SecretKey = config.SecretKey()
}
if config.TokenKey() != "" {
metadata.TokenKey = config.TokenKey()
}
if config.ClusterName() != "" {
metadata.Name = config.ClusterName()
}
if config.ClusterID() != "" {
metadata.ID = config.ClusterID()
}

rancherClient, err := newRancherClient(rancher_client.RancherConfig{
RancherURL: metadata.RancherURL,
AccessKey: metadata.AccessKey,
SecretKey: metadata.SecretKey,
Insecure: config.InsecureAPI(),
CACerts: config.CACerts(),
})
if err != nil {
return nil, err
}
return rancherClient, nil
}

func fillRancherMetadata(metadata *rancherModel.RancherMetadata, config config.Config) (rancher_client.RancherConfig, error) {
if config.RancherURL() != "" {
metadata.RancherURL = config.RancherURL()
}
if config.AccessKey() != "" {
metadata.AccessKey = config.AccessKey()
}
if config.SecretKey() != "" {
metadata.SecretKey = config.SecretKey()
}
if config.TokenKey() != "" {
metadata.TokenKey = config.TokenKey()
}

return rancher_client.RancherConfig{
RancherURL: metadata.RancherURL,
AccessKey: metadata.AccessKey,
SecretKey: metadata.SecretKey,
Insecure: config.InsecureAPI(),
CACerts: config.CACerts(),
}, nil
}
2 changes: 1 addition & 1 deletion internal/pkg/rancher/client/app_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"reflect"
"strings"

projectModel "github.com/bitgrip/cattlectl/internal/pkg/rancher/project/model"
projectModel "github.com/bitgrip/cattlectl/internal/pkg/rancher/cluster/project/model"
"github.com/rancher/norman/types"
backendProjectClient "github.com/rancher/types/client/project/v3"
"github.com/sirupsen/logrus"
Expand Down
Loading

0 comments on commit e0c1da7

Please sign in to comment.