Skip to content

enhance matching #193

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ build:
export CGO_ENABLED=0 && \
go build -o bin/${HELM_PLUGIN_NAME} -ldflags $(LDFLAGS) ./cmd/mapkubeapis

.PHONY: converter
converter:
export CGO_ENABLED=0 && \
go build -o bin/converter ./cmd/converter

.PHONY: bootstrap
bootstrap:
export GO111MODULE=on && \
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

> Note: Charts need to be updated also to supported Kubernetes APIs to avoid failure during deployment in a Kubernetes version. This is a separate task to the plugin.

> Note: The mapfile format has changed from the previous release. The new format is more resilient to changes in spacing and ordering of the Helm release metadata. The new format is described in the [API Mapping](#api-mapping) section. A converter has been provided to convert the old format to the new format. The converter may be found in the `cmd/converter` directory, built via `make converter`, and ran via `./bin/converter`.

## Prerequisite

- Helm client with `mapkubeapis` plugin installed on the same system
Expand Down Expand Up @@ -95,10 +97,14 @@ kind: ClusterRoleBinding
The mapping information of deprecated or removed APIs to supported APIs is configured in the [Map.yaml](https://github.com/helm/helm-mapkubeapis/blob/master/config/Map.yaml) file. The file is a list of entries similar to the following:

```yaml
- deprecatedAPI: "apiVersion: extensions/v1beta1\nkind: Deployment"
newAPI: "apiVersion: apps/v1\nkind: Deployment"
deprecatedInVersion: "v1.9"
removedInVersion: "v1.16"
- deprecatedAPI:
apiVersion: extensions/v1beta1
kind: Deployment
newAPI:
apiVersion: apps/v1
kind: Deployment
deprecatedInVersion: "v1.9"
removedInVersion: "v1.16"
```

The plugin when performing update of a Helm release metadata first loads the map file from the `config` directory where the plugin is run from. If the map file is a different name or in a different location, you can use the `--mapfile` flag to specify the different mapping file.
Expand Down
81 changes: 81 additions & 0 deletions cmd/converter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/helm/helm-mapkubeapis/pkg/common"
"github.com/helm/helm-mapkubeapis/pkg/mapping"
"sigs.k8s.io/yaml"
)

func readConfig(f string) ([]common.GenericYAML, error) {
b, err := os.ReadFile(f)
if err != nil {
return nil, err
}
return common.ParseYAML(string(b))
}

func exitOnError(err error) {
if err != nil {
_, _ = os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}

func main() {
var config string
if len(os.Args) == 1 || strings.HasPrefix(os.Args[1], "-h") || strings.HasPrefix(os.Args[1], "--h") {
_, _ = os.Stdout.WriteString("Usage: " + os.Args[0] + " [file]\n")
os.Exit(0)
}
config = os.Args[1]
input, err := readConfig(config)
exitOnError(err)
var mappings []any
if v, ok := input[0]["mappings"].([]any); ok {
mappings = v
}
if len(mappings) == 0 {
exitOnError(fmt.Errorf("failed to read mappings"))
}
var output mapping.Metadata
var m common.GenericYAML
var s string
var ok bool
for _, val := range mappings {
var om mapping.Mapping
if m, ok = val.(common.GenericYAML); !ok {
continue // skip invalid mappings
}
if s, ok = m["deprecatedAPI"].(string); ok {
apiVersion, kind := parseAPIString(s)
if apiVersion == "" || kind == "" {
continue // skip invalid mappings
}
om.DeprecatedAPI.APIVersion = apiVersion
om.DeprecatedAPI.Kind = kind
}
if s, ok = m["newAPI"].(string); ok {
apiVersion, kind := parseAPIString(s)
if apiVersion == "" || kind == "" {
continue // skip invalid mappings
}
om.NewAPI.APIVersion = apiVersion
om.NewAPI.Kind = kind
}
if s, ok = m["deprecatedInVersion"].(string); ok {
om.DeprecatedInVersion = s
}
if s, ok = m["removedInVersion"].(string); ok {
om.RemovedInVersion = s
}
output.Mappings = append(output.Mappings, &om)
}
var b []byte
b, err = yaml.Marshal(output)
exitOnError(err)
_, _ = os.Stdout.Write(b)
}
33 changes: 33 additions & 0 deletions cmd/converter/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import "strings"

const (
apiVersionLabel = "apiVersion:"
kindLabel = "kind:"
apiVersionLabelSize = len(apiVersionLabel)
kindLabelSize = len(kindLabel)
)

// parseAPIString parses the API string into version and kind components
func parseAPIString(apiString string) (version, kind string) {
idx := strings.Index(apiString, apiVersionLabel)
if idx != -1 {
temps := apiString[idx+apiVersionLabelSize:]
idx = strings.Index(temps, "\n")
if idx != -1 {
temps = temps[:idx]
version = strings.TrimSpace(temps)
}
}
idx = strings.Index(apiString, kindLabel)
if idx != -1 {
temps := apiString[idx+kindLabelSize:]
idx = strings.Index(temps, "\n")
if idx != -1 {
temps = temps[:idx]
kind = strings.TrimSpace(temps)
}
}
return version, kind
}
Loading