Skip to content

Commit

Permalink
feat: Controllers Code (#17) (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsalles authored Nov 11, 2021
1 parent 93959fc commit 0303718
Show file tree
Hide file tree
Showing 22 changed files with 1,035 additions and 115 deletions.
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ go 1.17

require (
github.com/gin-gonic/gin v1.7.4
github.com/google/uuid v1.1.2
github.com/kelseyhightower/envconfig v1.4.0
github.com/prometheus/client_golang v1.11.0
github.com/sirupsen/logrus v1.8.1
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.27.1
k8s.io/api v0.22.2
k8s.io/apimachinery v0.22.2
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b
sigs.k8s.io/controller-runtime v0.10.2
Expand All @@ -18,16 +21,19 @@ require (
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
Expand All @@ -38,6 +44,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 // indirect
Expand All @@ -46,13 +53,15 @@ require (
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/api v0.22.2 // indirect
k8s.io/apiextensions-apiserver v0.22.2 // indirect
k8s.io/client-go v0.22.2 // indirect
k8s.io/component-base v0.22.2 // indirect
k8s.io/klog/v2 v2.9.0 // indirect
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4er
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand Down Expand Up @@ -258,6 +259,8 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down Expand Up @@ -436,6 +439,7 @@ go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqe
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
Expand Down Expand Up @@ -476,6 +480,7 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
Expand Down Expand Up @@ -642,6 +647,7 @@ golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.2 h1:kRBLX7v7Af8W7Gdbbc908OJcdgtK8bOz9Uaj8/F1ACA=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -767,6 +773,7 @@ k8s.io/apiserver v0.22.2/go.mod h1:vrpMmbyjWrgdyOvZTSpsusQq5iigKNWv9o9KlDAbBHI=
k8s.io/client-go v0.22.2 h1:DaSQgs02aCC1QcwUdkKZWOeaVsQjYvWv8ZazcZ6JcHc=
k8s.io/client-go v0.22.2/go.mod h1:sAlhrkVDf50ZHx6z4K0S40wISNTarf1r800F+RlCF6U=
k8s.io/code-generator v0.22.2/go.mod h1:eV77Y09IopzeXOJzndrDyCI88UBok2h6WxAlBwpxa+o=
k8s.io/component-base v0.22.2 h1:vNIvE0AIrLhjX8drH0BgCNJcR4QZxMXcJzBsDplDx9M=
k8s.io/component-base v0.22.2/go.mod h1:5Br2QhI9OTe79p+TzPe9JKNQYvEKbq9rTJDWllunGug=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/gengo v0.0.0-20201214224949-b6c5ce23f027/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
Expand Down
54 changes: 54 additions & 0 deletions pkg/common/clients/storage_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package clients

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
)

// StorageClient is a structure that holds all the dependencies for the following client
type StorageClient struct {
logger *logrus.Logger
configuration StorageClientConfiguration
}

// NewStorageClient returns an initialized struct with the required dependencies injected
func NewStorageClient(logger *logrus.Logger, configuration StorageClientConfiguration) StorageClient {
return StorageClient{
logger: logger,
configuration: configuration,
}
}

// CreateWorkspace attempts to create a directory to hold requirements.txt
func (sc StorageClient) CreateWorkspace(obj, relativePath string) error {
fullPath := fmt.Sprintf("%s/%s/%s", sc.configuration.BasePath, obj, relativePath)

sc.logger.Info("Attempting to create directory ", fullPath)

if _, err := os.Stat(fullPath); os.IsNotExist(err) {
return os.MkdirAll(fullPath, os.ModePerm)
}

return nil
}

// CheckIfExists attempts to check if the directory exists
func (sc *StorageClient) CheckIfExists(obj, relativePath string) bool {
fullPath := fmt.Sprintf("%s/%s/%s", sc.configuration.BasePath, obj, relativePath)

_, err := os.Stat(fullPath)
if err != nil && os.IsNotExist(err) {
return false
}

return true
}

// RemoveDirectory attempts to remove the directory that holds requirements.txt
func (sc StorageClient) RemoveDirectory(obj, relativePath string) error {
fullPath := fmt.Sprintf("%s/%s/%s", sc.configuration.BasePath, obj, relativePath)
sc.logger.Info("Attempting to remove ", fullPath)
return os.RemoveAll(fullPath)
}
15 changes: 15 additions & 0 deletions pkg/common/clients/storage_client_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package clients

import (
"github.com/kelseyhightower/envconfig"
)

// StorageClientConfiguration is a struct that holds all the environment variables required to the Storage Client
type StorageClientConfiguration struct {
BasePath string `envconfig:"STORAGE_PATH" required:"true"`
}

// LoadFromEnvVars parses the required configuration variables. Throws an error if the validations aren't met
func (c *StorageClientConfiguration) LoadFromEnvVars() error {
return envconfig.Process("", c)
}
86 changes: 16 additions & 70 deletions pkg/common/config/configuration_variables.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,30 @@
package common

import (
"fmt"
"os"
"strconv"
)
package config

// ConfigurationVariables is an interface with LoadFromEnvVars method. The method is meant to be implemented as a struct
// method:
//
// type StructWithVars struct {
// var1 string
// var2 string
// Var1 string `envconfig:"VAR1"`
// Var2 string `envconfig:"VAR2"`
// }
//
// func (swv *StructWithVars) LoadFromEnvVars() error {
// swv.var1 = os.Getenv("VALUE_1")
// swv.var2 = "value 2"
// }
// func (swv *StructWithVars) LoadFromEnvVars() error {
// if err := envconfig.Process("", swv); err != nil {
// return err
// }
// return nil
// }
//
type ConfigurationVariables interface {
LoadFromEnvVars() error
}

// VariableFromEnvironment check from the value in the os.Getenv and returns it of error if doesn't exists.
func VariableFromEnvironment(env string) (string, error) {
value := os.Getenv(env)
if value == "" {
return "", fmt.Errorf("%s variable not set", env)
}

return value, nil
}

// BooleanVariableFromEnvironment check from the value in the os.Getenv, converts to boolean returns it of error if doesn't exists.
func BooleanVariableFromEnvironment(env string) (bool, error) {
value, err := VariableFromEnvironment(env)
if err != nil {
return false, err
}

booleanValue, err := strconv.ParseBool(value)
if err != nil {
return false, err
}

return booleanValue, nil
}

// IntVariableFromEnvironment check from the value in the os.Getenv and returns it of error if doesn't exists.
func IntVariableFromEnvironment(env string) (int, error) {
value, err := VariableFromEnvironment(env)
if err != nil {
return 0, err
// InitConfigurationVariables according to environment
func InitConfigurationVariables(configs []ConfigurationVariables) error {
for _, configuration := range configs {
if err := configuration.LoadFromEnvVars(); err != nil {
return err
}
}

intValue, err := strconv.Atoi(value)
if err != nil {
return 0, err
}

return intValue, nil
}

// Int32VariableFromEnvironment check from the value in the os.Getenv and returns it of error if doesn't exists.
func Int32VariableFromEnvironment(env string) (int32, error) {
value, err := IntVariableFromEnvironment(env)
if err != nil {
return 0, err
}

return int32(value), nil
}

// Int64VariableFromEnvironment check from the value in the os.Getenv and returns it of error if doesn't exists.
func Int64VariableFromEnvironment(env string) (int64, error) {
value, err := IntVariableFromEnvironment(env)
if err != nil {
return 0, err
}

return int64(value), nil
return nil
}
43 changes: 0 additions & 43 deletions pkg/common/config/environment.go

This file was deleted.

19 changes: 19 additions & 0 deletions pkg/common/config/manager_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"github.com/kelseyhightower/envconfig"
)

// ManagerConfiguration defines required variables to configure the environment
type ManagerConfiguration struct {
EnableLeaderElection bool `envconfig:"ENABLE_LEADER_ELECTION" required:"true"`
LeaderElectionID string `envconfig:"LEADER_ELECTION_ID" required:"true"`
Port int `envconfig:"MANAGER_PORT" default:"9443"`
MetricsServerPort int `envconfig:"METRICS_SERVER_PORT" default:"8080"`
HealthProbeAddress string `envconfig:"HEALTH_PROBE_ADDRESS" default:":8081"`
}

// LoadFromEnvVars from the Manager
func (c *ManagerConfiguration) LoadFromEnvVars() error {
return envconfig.Process("", c)
}
19 changes: 19 additions & 0 deletions pkg/common/config/rest_controller_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"time"

"github.com/kelseyhightower/envconfig"
)

// RESTControllerConfiguration defines a struct with required environment variables for rest controller
type RESTControllerConfiguration struct {
UserID string `envconfig:"USER_ID" required:"true"`
UserIDPrefix string `envconfig:"USER_ID_PREFIX" default:""`
HTTPRequestTimeout time.Duration `envconfig:"HTTP_REQUEST_TIMEOUT" default:"30s"`
}

// LoadFromEnvVars reads all env vars required
func (c *RESTControllerConfiguration) LoadFromEnvVars() error {
return envconfig.Process("", c)
}
8 changes: 7 additions & 1 deletion pkg/common/config/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package config

// UID defines UID type represents a ID for a specific resource
type UID string
Expand All @@ -8,3 +8,9 @@ type Namespace string

// UserID defines the user id type
type UserID string

// Credentials store the credentials from vault
type Credentials map[string]string

// ResourceKind defines a resource kind type
type ResourceKind string
Loading

0 comments on commit 0303718

Please sign in to comment.