Skip to content

Commit

Permalink
Import openshift-pipelines/operator-tooling in…
Browse files Browse the repository at this point in the history
… so that it is encapsulated with the operator's code.

Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
  • Loading branch information
vdemeester authored and tekton-robot committed Jun 16, 2023
1 parent 8a77ead commit bd5d238
Show file tree
Hide file tree
Showing 205 changed files with 31,857 additions and 22 deletions.
13 changes: 4 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ CR = config/basic
PLATFORM := $(if $(PLATFORM),--platform $(PLATFORM))

GOLANGCI_VERSION = v1.47.2
OPERATOR_TOOLING_VERSION = 0.0.2

BIN = $(CURDIR)/.bin

Expand Down Expand Up @@ -45,10 +44,6 @@ GOLANGCILINT = $(or ${GOLANGCILINT_BIN},${GOLANGCILINT_BIN},$(BIN)/golangci-lint
$(BIN)/golangci-lint: | $(BIN) ; $(info $(M) getting golangci-lint $(GOLANGCI_VERSION))
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(BIN) $(GOLANGCI_VERSION)

OPERATORTOOL = $(or ${OPERATORTOOL_BIN},${OPERATORTOOL_BIN},$(BIN)/operator-tool)
$(BIN)/operator-tool: | $(BIN) ; $(info $(M) getting operator-tool $(OPERATOR_TOOLING_VERSION))
curl -sSfL https://github.com/openshift-pipelines/operator-tooling/releases/download/v$(OPERATOR_TOOLING_VERSION)/operator-tool_${OPERATOR_TOOLING_VERSION}_Linux_x86_64.tar.gz | tar -xz -C $(BIN)

.PHONY: clean-cluster
clean-cluster: | $(KO) $(KUSTOMIZE) clean-cr; $(info $(M) clean $(TARGET)…) @ ## Cleanup cluster
@ ## --load-restrictor LoadRestrictionsNone is needed in kustomize build as files which not in child tree of kustomize base are pulled
Expand Down Expand Up @@ -100,15 +95,15 @@ bin/%: cmd/% FORCE

.PHONY: compoments/bump
components/bump: $(OPERATORTOOL)
@$(OPERATORTOOL) -config components.yaml bump
@go run ./cmd/tool bump components.yaml

.PHONY: components/bump-bugfix
components/bump-bugfix: $(OPERATORTOOL)
@$(OPERATORTOOL) -config components.yaml bump-bugfix
@go run ./cmd/tool --bugfix components.yaml

.PHONY: get-releases
get-releases: $(OPERATORTOOL) |
$Q ./hack/fetch-releases.sh $(OPERATORTOOL) $(TARGET) components.yaml $(FORCE_FETCH_RELEASE) || exit ;
get-releases: |
$Q ./hack/fetch-releases.sh $(TARGET) components.yaml $(FORCE_FETCH_RELEASE) || exit ;

.PHONY: apply
apply: | $(KO) $(KUSTOMIZE) get-releases ; $(info $(M) ko apply on $(TARGET)) @ ## Apply config to the current cluster
Expand Down
64 changes: 64 additions & 0 deletions cmd/tool/bump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Package main is the main package :D
package main

import (
"fmt"
"sort"

"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
"github.com/spf13/cobra"
)

var bugfix bool

func BumpCommand(ioStreams *cli.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "bump",
Short: "Bump components version",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("Requires 1 argument")
}
filename := args[0]
return bump(filename, bugfix)
},
Annotations: map[string]string{
"commandType": "main",
},
}
cmd.Flags().BoolVar(&bugfix, "bugfix", false, "Only update bugfix versions of components")
return cmd
}

func bump(filename string, bugfix bool) error {
newComponents := map[string]component{}
components, err := ReadCompoments(filename)
if err != nil {
return err
}
for name, component := range components {
newComponent, err := bumpComponent(name, component, bugfix)
if err != nil {
return err
}
newComponents[name] = newComponent
}
return writeComponents(filename, newComponents)
}

func bumpComponent(name string, c component, bugfix bool) (component, error) {
newVersion := c.Version
newerVersions, err := checkComponentNewerVersions(c, bugfix)
if err != nil {
return component{}, err
}
if len(newerVersions) > 0 {
// Get the latest one
sort.Sort(newerVersions) // sort just in case
newVersion = "v" + newerVersions[len(newerVersions)-1].String()
}
return component{
Github: c.Github,
Version: newVersion,
}, nil
}
123 changes: 123 additions & 0 deletions cmd/tool/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"context"
"fmt"
"io"
"sort"

"github.com/Masterminds/semver"
"github.com/cli/go-gh"
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

func CheckCommand(ioStreams *cli.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "check",
Short: "Check component versions (and if there is an upgrade needed)",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("Requires 1 argument")
}
filename := args[0]
return check(filename, bugfix, ioStreams.Out)
},
Annotations: map[string]string{
"commandType": "main",
},
}
cmd.Flags().BoolVar(&bugfix, "bugfix", false, "Only update bugfix versions of components")
return cmd
}

func check(filename string, bugfix bool, out io.Writer) error {
components, err := ReadCompoments(filename)
if err != nil {
return err
}
g, ctx := errgroup.WithContext(context.Background())
for name, component := range components {
// Force scope
name := name
component := component

g.Go(func() error {
return checkComponent(ctx, name, component, bugfix, out)
})
}
return g.Wait()
}

func checkComponent(ctx context.Context, name string, component component, bugfix bool, out io.Writer) error {
newerVersion, err := checkComponentNewerVersions(component, bugfix)
if err != nil {
return err
}
if len(newerVersion) > 0 {
fmt.Fprintf(out, "%s: %v\n", name, newerVersion)
}

return nil
}

func checkComponentNewerVersions(component component, bugfix bool) (semver.Collection, error) {
sVersions, err := fetchVersions(component.Github)
if err != nil {
return nil, err
}
currentVersion, err := semver.NewVersion(component.Version)
if err != nil {
return nil, err
}
newerVersion, err := getNewerVersion(currentVersion, sVersions, bugfix)
if err != nil {
return nil, err
}
return newerVersion, nil
}

func fetchVersions(github string) (semver.Collection, error) {
client, err := gh.RESTClient(nil)
if err != nil {
return nil, err
}
versions := []struct {
Name string
TagName string `json:"tag_name"`
}{}
err = client.Get(fmt.Sprintf("repos/%s/releases", github), &versions)
if err != nil {
return nil, err
}
sVersions := semver.Collection([]*semver.Version{})
for _, v := range versions {
sVersion, err := semver.NewVersion(v.TagName)
if err != nil {
return nil, err
}
sVersions = append(sVersions, sVersion)
}
sort.Sort(sVersions)
return sVersions, nil
}

func getNewerVersion(currentVersion *semver.Version, versions []*semver.Version, bugfix bool) (semver.Collection, error) {
constraint := fmt.Sprintf("> %s", currentVersion)
if bugfix {
nextMinorVersion := currentVersion.IncMinor()
constraint = fmt.Sprintf("> %s, < %s", currentVersion, nextMinorVersion.String())
}
c, err := semver.NewConstraint(constraint)
if err != nil {
return nil, err
}
newerVersion := semver.Collection([]*semver.Version{})
for _, sv := range versions {
if c.Check(sv) {
newerVersion = append(newerVersion, sv)
}
}
return newerVersion, nil
}
69 changes: 69 additions & 0 deletions cmd/tool/components.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"fmt"
"io"
"os"

"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)

type component struct {
Github string `json:"github"`
Version string `json:"version"`
}

func ReadCompoments(filename string) (map[string]component, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}

components := map[string]component{}
if err := yaml.Unmarshal(data, &components); err != nil {
return nil, err
}
return components, nil
}

func writeComponents(filename string, components map[string]component) error {
data, err := yaml.Marshal(components)
if err != nil {
return err
}
return os.WriteFile(filename, data, 0o644)
}

func ComponentVersionCommand(ioStreams *cli.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "component-version",
Short: "Prints the version of a component from a components file",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("Requires at least 2 argument")
}
filename := args[0]
return componentVersion(filename, args[1:], ioStreams.Out)
},
}
return cmd
}

func componentVersion(filename string, args []string, out io.Writer) error {
if len(args) == 0 || len(args) > 1 {
return fmt.Errorf("Need one and only one argument, the component name")
}
component := args[0]
components, err := ReadCompoments(filename)
if err != nil {
return err
}
c, ok := components[component]
if !ok {
return fmt.Errorf("Component %s not found", component)
}
fmt.Fprint(out, c.Version)
return nil
}
30 changes: 30 additions & 0 deletions cmd/tool/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"os"

"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
"github.com/spf13/cobra"
)

func main() {
cmd := &cobra.Command{
Use: "operator-tool",
Short: "Tooling to manage the operator",
Long: `This is a tool to help maintaining this operator`,
SilenceUsage: true,
Annotations: map[string]string{
"commandType": "main",
},
}

ioStreams := cli.NewIOStreams()

cmd.AddCommand(BumpCommand(ioStreams))
cmd.AddCommand(CheckCommand(ioStreams))
cmd.AddCommand(ComponentVersionCommand(ioStreams))

if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
Loading

0 comments on commit bd5d238

Please sign in to comment.