Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add setHeader functionality for httpRoutes #31

Merged
merged 11 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches:
- "main"
env:
GOLANG_VERSION: '1.19'
GOLANG_VERSION: '1.20'

jobs:
unit-tests:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- "release-v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+"

env:
GOLANG_VERSION: "1.19"
GOLANG_VERSION: "1.20"

jobs:
release-creation:
Expand Down
3 changes: 3 additions & 0 deletions internal/defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package defaults

const ConfigMap = "argo-gatewayapi-configmap"
119 changes: 119 additions & 0 deletions internal/utils/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package utils

import (
"encoding/json"
"strings"

pluginTypes "github.com/argoproj/argo-rollouts/utils/plugin/types"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
kubeErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

func GetKubeConfig() (*rest.Config, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// if you want to change the loading rules (which files in which order), you can do so here
configOverrides := &clientcmd.ConfigOverrides{}
// if you want to change override values or bind them to flags, there are methods to help you
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
config, err := kubeConfig.ClientConfig()
if err != nil {
return nil, pluginTypes.RpcError{ErrorString: err.Error()}
}
return config, nil
}

func SetLogLevel(logLevel string) {
level, err := log.ParseLevel(logLevel)
if err != nil {
log.Fatal(err)
}
log.SetLevel(level)
}

func CreateFormatter(logFormat string) log.Formatter {
var formatType log.Formatter
switch strings.ToLower(logFormat) {
case "json":
formatType = &log.JSONFormatter{}
case "text":
formatType = &log.TextFormatter{
FullTimestamp: true,
}
default:
log.Infof("Unknown format: %s. Using text logformat", logFormat)
formatType = &log.TextFormatter{
FullTimestamp: true,
}
}
return formatType
}

func CreateConfigMap(name string, options CreateConfigMapOptions) (*v1.ConfigMap, error) {
clientset := options.Clientset
ctx := options.Ctx
configMap, err := clientset.Get(ctx, name, metav1.GetOptions{})
if err != nil && !kubeErrors.IsNotFound(err) {
return nil, err
}
if err == nil {
return configMap, err
}
configMap.Name = name
configMap, err = clientset.Create(ctx, configMap, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return configMap, err
}

func GetConfigMapData(configMap *v1.ConfigMap, configMapKey string, destination any) error {
if configMap.Data != nil && configMap.Data[configMapKey] != "" {
err := json.Unmarshal([]byte(configMap.Data[configMapKey]), &destination)
if err != nil {
return err
}
}
return nil
}

func UpdateConfigMapData(configMap *v1.ConfigMap, configMapData any, options UpdateConfigMapOptions) error {
clientset := options.Clientset
rawConfigMapData, err := json.Marshal(configMapData)
if err != nil {
return err
}
if configMap.Data == nil {
configMap.Data = make(map[string]string)
}
configMap.Data[options.ConfigMapKey] = string(rawConfigMapData)
_, err = clientset.Update(options.Ctx, configMap, metav1.UpdateOptions{})
return err
}

func RemoveIndex[T any](original []T, index int) []T {
result := original[:index]
return append(result, original[index+1:]...)
}

func DoTransaction(logCtx *log.Entry, taskList ...Task) error {
var err, reverseErr error
for index, task := range taskList {
err = task.Action()
if err == nil {
continue
}
logCtx.Error(err.Error())
for i := index - 1; i > -1; i-- {
reverseErr = taskList[i].ReverseAction()
if err != nil {
return reverseErr
}
}
return err
}
return nil
}
23 changes: 23 additions & 0 deletions internal/utils/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package utils

import (
"context"

v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

type CreateConfigMapOptions struct {
Clientset v1.ConfigMapInterface
Ctx context.Context
}

type UpdateConfigMapOptions struct {
Clientset v1.ConfigMapInterface
ConfigMapKey string
Ctx context.Context
}

type Task struct {
Action func() error
ReverseAction func() error
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"github.com/argoproj-labs/rollouts-plugin-trafficrouter-gatewayapi/internal/utils"
"github.com/argoproj-labs/rollouts-plugin-trafficrouter-gatewayapi/pkg/plugin"
"github.com/argoproj-labs/rollouts-plugin-trafficrouter-gatewayapi/utils"

rolloutsPlugin "github.com/argoproj/argo-rollouts/rollout/trafficrouting/plugin/rpc"
goPlugin "github.com/hashicorp/go-plugin"
Expand Down
39 changes: 30 additions & 9 deletions pkg/mocks/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mocks

import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/gateway-api/apis/v1alpha2"
Expand All @@ -9,18 +10,26 @@ import (
)

const (
HTTPRoute = "HTTPRoute"
TCPRoute = "TCPRoute"
StableServiceName = "argo-rollouts-stable-service"
CanaryServiceName = "argo-rollouts-canary-service"
HTTPRouteName = "argo-rollouts-http-route"
TCPRouteName = "argo-rollouts-tcp-route"
Namespace = "default"
HTTPRoute = "HTTPRoute"
TCPRoute = "TCPRoute"
StableServiceName = "argo-rollouts-stable-service"
CanaryServiceName = "argo-rollouts-canary-service"
HTTPRouteName = "argo-rollouts-http-route"
TCPRouteName = "argo-rollouts-tcp-route"
Namespace = "default"
ConfigMapName = "test-config"
HTTPManagedRouteName = "test-http-header-route"
)

var (
port = v1beta1.PortNumber(80)
weight int32 = 0
port = v1beta1.PortNumber(80)
weight int32 = 0
httpPathMatchType = v1beta1.PathMatchPathPrefix
httpPathMatchValue = "/"
httpPathMatch = v1beta1.HTTPPathMatch{
Type: &httpPathMatchType,
Value: &httpPathMatchValue,
}
)

var HTTPRouteObj = v1beta1.HTTPRoute{
Expand Down Expand Up @@ -58,6 +67,11 @@ var HTTPRouteObj = v1beta1.HTTPRoute{
},
},
},
Matches: []v1beta1.HTTPRouteMatch{
{
Path: &httpPathMatch,
},
},
},
},
},
Expand Down Expand Up @@ -98,3 +112,10 @@ var TCPPRouteObj = v1alpha2.TCPRoute{
},
},
}

var ConfigMapObj = v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: ConfigMapName,
Namespace: Namespace,
},
}
12 changes: 12 additions & 0 deletions pkg/plugin/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package plugin

const (
GatewayAPIUpdateError = "error updating Gateway API %q: %s"
GatewayAPIManifestError = "httpRoute and tcpRoute fields are empty. tcpRoute or httpRoute should be set"
HTTPRouteFieldIsEmptyError = "httpRoute field is empty. It has to be set to remove managed routes"
InvalidHeaderMatchTypeError = "invalid header match type"
BackendRefWasNotFoundInHTTPRouteError = "backendRef was not found in httpRoute"
BackendRefWasNotFoundInTCPRouteError = "backendRef was not found in tcpRoute"
BackendRefListWasNotFoundInTCPRouteError = "backendRef list was not found in tcpRoute"
ManagedRouteMapEntryDeleteError = "can't delete key %q from managedRouteMap. The key %q is not in the managedRouteMap"
)
Loading
Loading