-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add setHeader functionality for httpRoutes (#31)
* feat: Add setHeader functionality for httpRoutes Signed-off-by: Philipp Plotnikov <[email protected]> * refactor: fix lint Signed-off-by: Philipp Plotnikov <[email protected]> * refactor: seperate logic setHTTPHeaderRoute to the different functions Signed-off-by: Philipp Plotnikov <[email protected]> * refactor: move error messages into seperate file Signed-off-by: Philipp Plotnikov <[email protected]> * refactor: rename mu field to the mutex field Signed-off-by: Philipp Plotnikov <[email protected]> * test: add tests for setHeaderRoute functionality Signed-off-by: Philipp Plotnikov <[email protected]> * feat: store data about httpManagedRoutes in configMap Signed-off-by: Philipp Plotnikov <[email protected]> * fix: fix getBackendRef function and add getRouteRule function Signed-off-by: Philipp Plotnikov <[email protected]> * refactor: add getGatewayAPITracfficRoutingConfig function Signed-off-by: Philipp Plotnikov <[email protected]> * fix: fix multiple managed route removement from map Signed-off-by: Philipp Plotnikov <[email protected]> * chore: up go version from 1.19 to 1.20 in github workflows Signed-off-by: Philipp Plotnikov <[email protected]> --------- Signed-off-by: Philipp Plotnikov <[email protected]>
- Loading branch information
1 parent
b9fb854
commit 139e9de
Showing
14 changed files
with
731 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ on: | |
branches: | ||
- "main" | ||
env: | ||
GOLANG_VERSION: '1.19' | ||
GOLANG_VERSION: '1.20' | ||
|
||
jobs: | ||
unit-tests: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package defaults | ||
|
||
const ConfigMap = "argo-gatewayapi-configmap" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
Oops, something went wrong.