Skip to content

Commit

Permalink
Merge branch 'release/v0.1.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
kahoona77 authored and cesmarvin committed Jul 13, 2023
2 parents 6de9dc6 + d5b534b commit 28a1cd1
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.1.1] - 2023-07-13

### Changed
- [#3] Improved doc comments and split complex components

## [v0.1.0] - 2023-03-27

### Added
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ RUN make compile-generic
FROM gcr.io/distroless/static:nonroot
LABEL maintainer="hello@cloudogu.com" \
NAME="k8s-host-change" \
VERSION="0.1.0"
VERSION="0.1.1"

WORKDIR /

Expand Down
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void stageAutomaticRelease() {
stage('Push to Registry') {
Makefile makefile = new Makefile(this)
String hostChangeVersion = makefile.getVersion()
GString targetOperatorResourceYaml = "target/${repositoryName}_${hostChangeVersion}.yaml"
GString targetOperatorResourceYaml = "target/make/k8s/${repositoryName}_${hostChangeVersion}.yaml"

DoguRegistry registry = new DoguRegistry(this)
registry.pushK8sYaml(targetOperatorResourceYaml, repositoryName, "k8s", "${hostChangeVersion}")
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Set these to the desired values
ARTIFACT_ID=k8s-host-change
VERSION=0.1.0
VERSION=0.1.1

GOTAG?=1.20.2
MAKEFILES_VERSION=7.5.0
Expand Down Expand Up @@ -58,6 +58,7 @@ run: ## Run a setup from your host.
.PHONY: k8s-create-temporary-resource
k8s-create-temporary-resource: $(K8S_RESOURCE_TEMP_FOLDER)
@cp $(K8S_HOST_CHANGE_RESOURCE_YAML) $(K8S_RESOURCE_TEMP_YAML)
@sed -i "s/'{{ .Version }}'/$(VERSION)/" $(K8S_RESOURCE_TEMP_YAML)

.PHONY: template-dev-only-image-pull-policy
template-dev-only-image-pull-policy: $(BINARY_YQ)
Expand Down
2 changes: 1 addition & 1 deletion k8s/k8s-host-change.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ spec:
spec:
containers:
- name: k8s-host-change
image: "cloudogu/k8s-host-change:0.0.1"
image: "cloudogu/k8s-host-change:'{{ .Version }}'"
env:
- name: STAGE
value: "production"
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"context"
"os"

ctrl "sigs.k8s.io/controller-runtime"

"github.com/cloudogu/k8s-host-change/pkg/hosts"
"github.com/cloudogu/k8s-host-change/pkg/initializer"
"github.com/cloudogu/k8s-host-change/pkg/logging"
"os"
ctrl "sigs.k8s.io/controller-runtime"
)

var logger = ctrl.Log.WithName("k8s-host-change")
Expand Down
4 changes: 3 additions & 1 deletion pkg/alias/hostAliasGenerator_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package alias

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

v1 "k8s.io/api/core/v1"
"k8s.io/utils/strings/slices"
"testing"
)

func Test_hostAliasGenerator_Generate(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/deployment/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package deployment
import (
"context"
"fmt"
"github.com/hashicorp/go-multierror"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"

"github.com/hashicorp/go-multierror"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
)

Expand Down
4 changes: 3 additions & 1 deletion pkg/deployment/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package deployment

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"testing"
)

const testNamespace = "ecosystem"
Expand Down
5 changes: 4 additions & 1 deletion pkg/dogu/deploymentFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package dogu
import (
"context"
"fmt"
"k8s.io/client-go/kubernetes"

appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// NewDeploymentFetcher creates a new instance of a deployment fetcher which is used for retrieving dogu deployments.
func NewDeploymentFetcher(clientSet kubernetes.Interface) *deploymentFetcher {
return &deploymentFetcher{clientSet: clientSet}
}
Expand All @@ -17,6 +18,8 @@ type deploymentFetcher struct {
clientSet kubernetes.Interface
}

// FetchAll retrieves all dogu deployments in a given namespace.
// The 'dogu.name' label key is used for identifying dogu deployments.
func (f *deploymentFetcher) FetchAll(ctx context.Context, namespace string) ([]appsv1.Deployment, error) {
selector := &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{{
Expand Down
13 changes: 11 additions & 2 deletions pkg/hosts/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package hosts
import (
"context"
"fmt"
"github.com/cloudogu/cesapp-lib/registry"

"github.com/hashicorp/go-multierror"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/cloudogu/cesapp-lib/registry"
"github.com/cloudogu/k8s-host-change/pkg/alias"
"github.com/cloudogu/k8s-host-change/pkg/deployment"
"github.com/cloudogu/k8s-host-change/pkg/dogu"
Expand Down Expand Up @@ -44,6 +43,16 @@ func (hau *defaultHostAliasUpdater) UpdateHosts(ctx context.Context, namespace s
logger.Info("Delete all aliases from dogu deployments")
}

err = hau.updateOrRollback(ctx, namespace, hostAliases)
if err != nil {
return err
}

return nil
}

func (hau *defaultHostAliasUpdater) updateOrRollback(ctx context.Context, namespace string, hostAliases []corev1.HostAlias) error {
logger := log.FromContext(ctx)
logger.Info("Fetch all dogu deployments")
deployments, err := hau.fetcher.FetchAll(ctx, namespace)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/hosts/change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package hosts

import (
"context"
"github.com/stretchr/testify/mock"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

appsv1 "k8s.io/api/apps/v1"
Expand Down
5 changes: 5 additions & 0 deletions pkg/hosts/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ package hosts

import (
"context"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)

type HostAliasUpdater interface {
// UpdateHosts updates all dogu deployments with host information like fqdn, internal ip and additional hosts from ces registry.
UpdateHosts(ctx context.Context, namespace string) error
}

type hostAliasGenerator interface {
// Generate patches the given deployment with the host configuration provided.
Generate() (hostAliases []corev1.HostAlias, err error)
}

type doguDeploymentFetcher interface {
// FetchAll retrieves all dogu deployments in a given namespace.
FetchAll(ctx context.Context, namespace string) ([]appsv1.Deployment, error)
}

type deploymentUpdater interface {
// UpdateHostAliases replaces the host aliases in the given deployments.
UpdateHostAliases(ctx context.Context, namespace string, deployments []appsv1.Deployment, aliases []corev1.HostAlias) error
}
16 changes: 13 additions & 3 deletions pkg/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package initializer

import (
"fmt"
"github.com/cloudogu/cesapp-lib/core"
"github.com/cloudogu/cesapp-lib/registry"
"k8s.io/client-go/kubernetes"
"os"

"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/cloudogu/cesapp-lib/core"
"github.com/cloudogu/cesapp-lib/registry"
)

const namespaceEnvName = "NAMESPACE"

// Initializer is used for populating this program with configuration values.
type Initializer interface {
// GetNamespace retrieves the namespace this program should work in.
GetNamespace() string
// CreateClientSet creates a client set from a kubernetes rest config.
CreateClientSet() (kubernetes.Interface, error)
// CreateCesRegistry creates a client for accessing ces configuration.
CreateCesRegistry() (registry.Registry, error)
}

Expand All @@ -24,6 +30,8 @@ var New = func() Initializer {
return &defaultInitializer{}
}

// GetNamespace retrieves the namespace this program should work in from the NAMESPACE environment variable.
// If the NAMESPACE var is not set or contains an empty string, the 'default' namespace is returned instead.
func (i *defaultInitializer) GetNamespace() string {
env, present := os.LookupEnv(namespaceEnvName)
if present && env != "" {
Expand All @@ -33,6 +41,7 @@ func (i *defaultInitializer) GetNamespace() string {
return "default"
}

// CreateClientSet creates a client set from a kubernetes rest config.
func (i *defaultInitializer) CreateClientSet() (kubernetes.Interface, error) {
restConfig := ctrl.GetConfigOrDie()
clientSet, err := kubernetes.NewForConfig(restConfig)
Expand All @@ -43,6 +52,7 @@ func (i *defaultInitializer) CreateClientSet() (kubernetes.Interface, error) {
return clientSet, nil
}

// CreateCesRegistry creates a client for accessing ces configuration.
func (i *defaultInitializer) CreateCesRegistry() (registry.Registry, error) {
namespace := i.GetNamespace()
cesReg, err := registry.New(core.Registry{
Expand Down
5 changes: 3 additions & 2 deletions pkg/initializer/initializer_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package initializer

import (
"os"
"testing"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd/api"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
17 changes: 13 additions & 4 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"os"

"github.com/bombsimon/logrusr/v2"
"github.com/cloudogu/cesapp-lib/core"
"github.com/go-logr/logr"
"github.com/sirupsen/logrus"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/cloudogu/cesapp-lib/core"
)

const namespaceLogLevel = "LOG_LEVEL"
const logLevelEnvName = "LOG_LEVEL"

const (
errorLevel int = iota
Expand All @@ -37,47 +38,55 @@ func (l *libraryLogger) logf(level int, format string, args ...interface{}) {
l.logger.Info(level, fmt.Sprintf("[%s] %s", l.name, fmt.Sprintf(format, args...)))
}

// Debug logs the given arguments with the debug log-level.
func (l *libraryLogger) Debug(args ...interface{}) {
l.log(debugLevel, args...)
}

// Info logs the given arguments with the info log-level.
func (l *libraryLogger) Info(args ...interface{}) {
l.log(infoLevel, args...)
}

// Warning logs the given arguments with the warning log-level.
func (l *libraryLogger) Warning(args ...interface{}) {
l.log(warningLevel, args...)
}

// Error logs the given arguments with the error log-level.
func (l *libraryLogger) Error(args ...interface{}) {
l.log(errorLevel, args...)
}

// Debugf formats the arguments into the given format string and logs it with the debug log-level.
func (l *libraryLogger) Debugf(format string, args ...interface{}) {
l.logf(debugLevel, format, args...)
}

// Infof formats the arguments into the given format string and logs it with the info log-level.
func (l *libraryLogger) Infof(format string, args ...interface{}) {
l.logf(infoLevel, format, args...)
}

// Warningf formats the arguments into the given format string and logs it with the warning log-level.
func (l *libraryLogger) Warningf(format string, args ...interface{}) {
l.logf(warningLevel, format, args...)
}

// Errorf formats the arguments into the given format string and logs it with the error log-level.
func (l *libraryLogger) Errorf(format string, args ...interface{}) {
l.logf(errorLevel, format, args...)
}

func getLogLevelFromEnv() (logrus.Level, error) {
logLevel, found := os.LookupEnv(namespaceLogLevel)
logLevel, found := os.LookupEnv(logLevelEnvName)
if !found {
return logrus.ErrorLevel, nil
}

level, err := logrus.ParseLevel(logLevel)
if err != nil {
return logrus.ErrorLevel, fmt.Errorf("value of log environment variable [%s] is not a valid log level: %w", namespaceLogLevel, err)
return logrus.ErrorLevel, fmt.Errorf("value of log environment variable [%s] is not a valid log level: %w", logLevelEnvName, err)
}

return level, nil
Expand Down
Loading

0 comments on commit 28a1cd1

Please sign in to comment.