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

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
- Sets up a custom informer for listening for ElasticsearchCluster types
- Defines ElasticsearchCluster types
- Adds example manifests
- No control logic currently except debugging
  • Loading branch information
munnerz committed Mar 13, 2017
0 parents commit 74ab652
Show file tree
Hide file tree
Showing 20 changed files with 1,232 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cmd/app/colonel.go
@@ -0,0 +1,52 @@
package app

import (
"fmt"

"github.com/Sirupsen/logrus"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

intinformers "gitlab.jetstack.net/marshal/colonel/pkg/informers"
)

var (
known = map[string]InitFn{
"ElasticSearch": newElasticsearchController,
}
)

func Known() map[string]InitFn {
return known
}

type ControllerContext struct {
Client *kubernetes.Clientset
TPRClient *rest.RESTClient

InformerFactory informers.SharedInformerFactory
MarshalInformerFactory intinformers.SharedInformerFactory

Namespace string
Stop <-chan struct{}
}

type InitFn func(*ControllerContext) (bool, error)

func StartControllers(ctx *ControllerContext, fns map[string]InitFn, stop <-chan struct{}) error {
for n, fn := range fns {
logrus.Debugf("starting %s controller", n)

_, err := fn(ctx)

if err != nil {
return fmt.Errorf("error starting '%s' controller: %s", n, err.Error())
}
}

ctx.InformerFactory.Start(stop)
ctx.MarshalInformerFactory.Start(stop)

select {}
}
16 changes: 16 additions & 0 deletions cmd/app/elasticsearch.go
@@ -0,0 +1,16 @@
package app

import (
"gitlab.jetstack.net/marshal/colonel/pkg/elastic"
)

func newElasticsearchController(ctx *ControllerContext) (bool, error) {
go elastic.NewElasticsearch(
ctx.MarshalInformerFactory.V1().ElasticsearchCluster(),
ctx.InformerFactory.Extensions().V1beta1().Deployments(),
ctx.InformerFactory.Apps().V1beta1().StatefulSets(),
ctx.Client,
).Run(ctx.Stop)

return true, nil
}
135 changes: 135 additions & 0 deletions cmd/root.go
@@ -0,0 +1,135 @@
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"os"
"time"

"github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"

"flag"

"gitlab.jetstack.net/marshal/colonel/cmd/app"
"gitlab.jetstack.net/marshal/colonel/pkg/api/v1"
intinformers "gitlab.jetstack.net/marshal/colonel/pkg/informers"
"gitlab.jetstack.net/marshal/colonel/pkg/kube"
)

var cfgFile string

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "colonel",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,

// TODO: Refactor this function from this package
Run: func(cmd *cobra.Command, args []string) {
server := "http://localhost:8082"
cl, err := kube.NewKubernetesClient(server)

if err != nil {
logrus.Fatalf("error creating kubernetes client: %s", err.Error())
}

tprClient, err := kube.NewMarshalRESTClient(server)

if err != nil {
logrus.Fatalf("error creating third party resource client: %s", err.Error())
}

var obj v1.ElasticsearchClusterList
err = tprClient.
Get().
Resource("elasticsearchclusters").
Do().
Into(&obj)

logrus.Printf("obj: %+v", obj)
if err != nil {
logrus.Fatalf("error listing: %s", err.Error())
}

ctx := app.ControllerContext{
Client: cl,
TPRClient: tprClient,
InformerFactory: informers.NewSharedInformerFactory(cl, time.Second*30),
MarshalInformerFactory: intinformers.NewSharedInformerFactory(tprClient, time.Second*30),
Namespace: metav1.NamespaceAll,
Stop: make(<-chan struct{}),
}

err = app.StartControllers(
&ctx,
app.Known(),
ctx.Stop,
)

if err != nil {
logrus.Fatalf("error running controllers: %s", err.Error())
}
},
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.colonel.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

RootCmd.Flags().AddGoFlagSet(flag.CommandLine)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag
viper.SetConfigFile(cfgFile)
}

viper.SetConfigName(".colonel") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
76 changes: 76 additions & 0 deletions examples/es-cluster-example.yaml
@@ -0,0 +1,76 @@
apiVersion: alpha.marshal.io/v1
kind: ElasticsearchCluster
metadata:
name: test
spec:
# ElasticSearch settings
version: '5.2.0'
plugins:
- name: io.fabric8:elasticsearch-cloud-kubernetes:5.2.0

# Kubernetes image settings
# image:
# pullPolicy: IfNotPresent

nodePools:
- name: data
replicas: 2

roles:
- data

resources:
requests:
cpu: 1
memory: 2Gi
limits:
cpu: 2
memory: 4Gi

state:
# if stateful is true, we'll create a StatefulSet instead of a Deployment
stateful: true

# if stateful is false, persistence must also be false
persistence:
enabled: true
size: 10Gi
storageClass: ""

- name: client
replicas: 2

roles:
- client

resources:
requests:
cpu: 1
memory: 2Gi
limits:
cpu: 2
memory: 4Gi

persistence:
enabled: true
size: 10Gi
storageClass: ""

- name: master
replicas: 2

roles:
- master

resources:
requests:
cpu: 1
memory: 2Gi
limits:
cpu: 2
memory: 4Gi

persistence:
enabled: true
size: 10Gi
storageClass: ""
7 changes: 7 additions & 0 deletions examples/es-cluster-tpr.yaml
@@ -0,0 +1,7 @@
apiVersion: extensions/v1beta1
kind: ThirdPartyResource
metadata:
name: elasticsearch-cluster.alpha.marshal.io
description: "A specification of an Elasticsearch cluster"
versions:
- name: v1
28 changes: 28 additions & 0 deletions main.go
@@ -0,0 +1,28 @@
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"

"github.com/Sirupsen/logrus"
"gitlab.jetstack.net/marshal/colonel/cmd"
)

func main() {
flag.Parse()
logrus.SetLevel(logrus.DebugLevel)
cmd.Execute()
}
40 changes: 40 additions & 0 deletions pkg/api/v1/meta.go
@@ -0,0 +1,40 @@
package v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type ElasticsearchCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`

Spec ElasticsearchClusterSpec `json:"spec"`
}

type ElasticsearchClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`

Items []ElasticsearchCluster `json:"items"`
}

// Required to satisfy Object interface
func (e *ElasticsearchCluster) GetObjectKind() schema.ObjectKind {
return &e.TypeMeta
}

// Required to satisfy ObjectMetaAccessor interface
func (e *ElasticsearchCluster) GetObjectMeta() metav1.Object {
return &e.ObjectMeta
}

// Required to satisfy Object interface
func (el *ElasticsearchClusterList) GetObjectKind() schema.ObjectKind {
return &el.TypeMeta
}

// Required to satisfy ListMetaAccessor interface
func (el *ElasticsearchClusterList) GetListMeta() metav1.List {
return &el.ListMeta
}

0 comments on commit 74ab652

Please sign in to comment.