Skip to content

Commit

Permalink
chore: remove KIC submodule (Kong/gateway-operator-archive#932)
Browse files Browse the repository at this point in the history
* chore: remove KIC submodule

* don't use git for fetching role files

* deduplicate control plane version consts

* fix: missing git tags v prefix

---------

Co-authored-by: Patryk Małek <[email protected]>
  • Loading branch information
czeslavo and pmalek authored Jul 31, 2023
1 parent 8568762 commit 28e3810
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 132 deletions.
3 changes: 0 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@
# Ignore build and test binaries.
bin/
testbin/

# Ignore KIC's submodule
kubernetes-ingress-controller/
1 change: 0 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ jobs:
- name: checkout repository
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0

- name: setup golang
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0

- name: Setup go
Expand Down Expand Up @@ -176,7 +175,6 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 0
submodules: true

- name: setup golang
uses: actions/setup-go@v4
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "kubernetes-ingress-controller"]
path = kubernetes-ingress-controller
url = https://github.com/Kong/kubernetes-ingress-controller.git
20 changes: 2 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@ ifndef COMMIT
COMMIT := $(shell git rev-parse --short HEAD)
endif

.PHONY: submodule
submodule: ## Initialize and update git submodules if needed
@-if ! scripts/verify-submodules-status.sh; then \
make submodule.init; \
fi

.PHONY: submodule.init
submodule.init: ## Initialize and update git submodules
git submodule sync --recursive
git submodule update --init --recursive

.PHONY: submodule.init-force
submodule.init-force: ## Reinitialize and update git submodules
git submodule sync --recursive
git submodule update --init --recursive --force

# ------------------------------------------------------------------------------
# Configuration - Build
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -100,7 +84,7 @@ envtest: ## Download envtest-setup locally if necessary.

KIC_ROLE_GENERATOR = $(PROJECT_DIR)/bin/kic-role-generator
.PHONY: kic-role-generator
kic-role-generator: submodule
kic-role-generator:
( cd ./hack/generators/kic-role-generator && go build -o $(KIC_ROLE_GENERATOR) . )

CONTROLLER_GEN = $(PROJECT_DIR)/bin/controller-gen
Expand Down Expand Up @@ -228,7 +212,7 @@ _build.operator:
main.go

.PHONY: build
build: generate submodule
build: generate
$(MAKE) build.operator

.PHONY: lint
Expand Down
2 changes: 2 additions & 0 deletions hack/generators/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/go-git/go-git/v5 v5.8.1
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/kong/gateway-operator v0.0.0-00010101000000-000000000000
k8s.io/api v0.27.4
k8s.io/apimachinery v0.27.4
Expand All @@ -28,6 +29,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
Expand Down
6 changes: 6 additions & 0 deletions hack/generators/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.4 h1:ZQgVdpTdAL7WpMIwLzCfbalOcSUdkDZnpUv3/+BxzFA=
github.com/hashicorp/go-retryablehttp v0.7.4/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
Expand Down
12 changes: 7 additions & 5 deletions hack/generators/kic-role-generator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"go/format"
"io"
"os"
"path"
"strings"
Expand All @@ -21,20 +22,21 @@ func gitCheckoutTag(repo *git.Repository, workTree *git.Worktree, version string
ref := plumbing.NewTagReferenceName(version)
reference, err := repo.Reference(ref, true)
if err != nil {
return err
return fmt.Errorf("failed getting reference for tag %s: %w", ref.String(), err)
}
if err = workTree.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(reference.Hash().String()),
Hash: plumbing.NewHash(reference.Hash().String()),
Force: true,
}); err != nil {
return err
return fmt.Errorf("failed checking out tag %s (%s): %w", version, reference.Hash(), err)
}

return nil
}

// parseRole unmarshals the config clusterrole file from the cloned kic repository
func parseRole(filePath string) (*rbacv1.ClusterRole, error) {
b, err := os.ReadFile(filePath)
func parseRole(file io.Reader) (*rbacv1.ClusterRole, error) {
b, err := io.ReadAll(file)
if err != nil {
return nil, err
}
Expand Down
101 changes: 30 additions & 71 deletions hack/generators/kic-role-generator/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package main

import (
"context"
"errors"
"flag"
"fmt"
"os"
"path"
"io"
"strings"

"github.com/Masterminds/semver"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/hashicorp/go-retryablehttp"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

kicversions "github.com/kong/gateway-operator/internal/versions"
)

const gitClonePath = "./kubernetes-ingress-controller"

var clusterRoleRelativePaths = []string{
"config/rbac/role.yaml",
"config/rbac/gateway/role.yaml",
Expand Down Expand Up @@ -50,71 +44,23 @@ func init() {
}

func main() {
gatewayRepo, err := git.PlainOpen(".")
exitOnErr(err, "failed opening '.'")

gatewayWorktree, err := gatewayRepo.Worktree()
exitOnErr(err, "failed getting gateway operator's work tree")

kicSubmodule, err := gatewayWorktree.Submodule("kubernetes-ingress-controller")
exitOnErr(err, "failed getting KIC's submodule")
kicStatus, err := kicSubmodule.Status()
exitOnErr(err, "failed getting KIC's submodule status")
if !kicStatus.IsClean() {
exitOnErr(
fmt.Errorf("status of kubernetes-ingress-controller submodule is not clean: %s", kicStatus))
}
prevHead := kicStatus.Current

err = kicSubmodule.Init()
if err != nil && !errors.Is(err, git.ErrSubmoduleAlreadyInitialized) {
exitOnErr(err, "failed initializing KIC's submodule")
}

err = kicSubmodule.UpdateContext(context.Background(),
&git.SubmoduleUpdateOptions{
Init: false,
NoFetch: true,
},
)
exitOnErr(err, "failed updating KIC's submodule")

kicRepo, err := kicSubmodule.Repository()
exitOnErr(err, "failed getting KIC's repository")

err = kicRepo.FetchContext(context.Background(), &git.FetchOptions{})
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
exitOnErr(err, "failed fetching for KIC's submodule repo")
}

kicWorktree, err := kicRepo.Worktree()
exitOnErr(err, "failed getting KIC's work tree")

if force {
exitOnErr(rmDirs(controllerRBACPath, kicRBACPath))
}

// defer reverting KIC's submodule back to status from before.
defer checkout(kicWorktree, prevHead)
for versionConstraint, rbacVersion := range kicversions.RoleVersionsForKICVersions {
fmt.Printf("INFO: checking and generating code for constraint %s with version %s\n", versionConstraint, rbacVersion)
// ensure the version has the "v" prefix
version := semver.MustParse(rbacVersion).String()
if !strings.HasPrefix(version, "v") {
version = fmt.Sprintf("v%s", version)
kicVersion := semver.MustParse(rbacVersion).String()
if !strings.HasPrefix(kicVersion, "v") {
kicVersion = fmt.Sprintf("v%s", kicVersion)
}
fmt.Printf("INFO: checking out tag %s\n", version)
exitOnErr(gitCheckoutTag(kicRepo, kicWorktree, version))

fmt.Printf("INFO: parsing clusterRole for KIC version %s\n", version)
fmt.Printf("INFO: parsing clusterRole for KIC version %s\n", kicVersion)
clusterRoles := []*rbacv1.ClusterRole{}
for _, rolePath := range clusterRoleRelativePaths {
// Here we try to merge all the rules from all known cluster roles.
rolePath := path.Join(gitClonePath, rolePath)
if _, err = os.Stat(rolePath); errors.Is(err, os.ErrNotExist) {
continue
}
newRole, err := parseRole(rolePath)
newRole, err := getRoleFromKICRepository(rolePath, kicVersion)
exitOnErr(err)
clusterRoles = append(clusterRoles, newRole)
}
Expand Down Expand Up @@ -198,18 +144,31 @@ func main() {
}
}

func checkout(workTree *git.Worktree, hash plumbing.Hash) {
err := workTree.Checkout(&git.CheckoutOptions{
Hash: hash,
Keep: false,
})
func getRoleFromKICRepository(filePath, version string) (*rbacv1.ClusterRole, error) {
file, err := getFileFromKICRepository(filePath, version)
if err != nil {
fmt.Printf(
"ERROR: failed to revert back kubernetes-ingress-controller submodule to %v: %v\n",
hash, err,
)
return nil, fmt.Errorf("failed to get %s from KIC repository: %w", filePath, err)
}
fmt.Printf("INFO: restored kubernetes-ingress-controller submodule back to %s\n", hash)
defer file.Close()

role, err := parseRole(file)
if err != nil {
return nil, fmt.Errorf("failed to parse role (%s) from KIC repository: %w", filePath, err)
}

return role, nil
}

func getFileFromKICRepository(filePath, version string) (io.ReadCloser, error) {
const baseKICRepoURLTemplate = "https://raw.githubusercontent.com/Kong/kubernetes-ingress-controller/%s/%s"

url := fmt.Sprintf(baseKICRepoURLTemplate, version, filePath)
resp, err := retryablehttp.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to get %s from KIC repository: %w", url, err)
}

return resp.Body, nil
}

func generatefile(
Expand Down
2 changes: 1 addition & 1 deletion hack/generators/kic-role-generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import (
// the version from the tag, and returns the ClusterRole with all the needed
// permissions.
func GenerateNewClusterRoleForControlPlane(controlplaneName string, image string) (*rbacv1.ClusterRole, error) {
versionToUse := consts.DefaultControlPlaneTag
versionToUse := versions.DefaultControlPlaneVersion
imageToUse := consts.DefaultControlPlaneImage
var constraint *semver.Constraints
Expand Down
9 changes: 3 additions & 6 deletions internal/consts/consts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package consts

import "github.com/kong/gateway-operator/internal/versions"

// ServiceType is a re-typing of string to be used to distinguish between proxy and admin service
type ServiceType string

Expand Down Expand Up @@ -41,14 +43,9 @@ const (
// decide an image fail.
DefaultControlPlaneBaseImage = "kong/kubernetes-ingress-controller"

// DefaultControlPlaneTag is the base container image tag that can be used
// by default for a ControlPlane resource if all other attempts to dynamically
// decide an image tag fail.
DefaultControlPlaneTag = "2.10.0" // TODO: automatic PR updates https://github.com/Kong/gateway-operator/issues/210

// DefaultControlPlaneImage is the default container image that can be used if
// all other attempts to dynamically decide the default image fail.
DefaultControlPlaneImage = DefaultControlPlaneBaseImage + ":" + DefaultControlPlaneTag
DefaultControlPlaneImage = DefaultControlPlaneBaseImage + ":" + versions.DefaultControlPlaneVersion

// ControlPlaneControllerContainerName is the name of the ingress controller container in a ControlPlane Deployment
ControlPlaneControllerContainerName = "controller"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/versions/controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

const (
// LatestClusterRoleVersion is the version of the ClusterRole that will be used for unversioned KIC
LatestClusterRoleVersion = "2.10.1"
// DefaultControlPlaneVersion is the default version of the ControlPlane to use.
// TODO: automatic PR updates https://github.com/Kong/gateway-operator/issues/210
DefaultControlPlaneVersion = "2.10.4"
)

// minimumControlPlaneVersion indicates the bare minimum version of the
Expand Down
1 change: 0 additions & 1 deletion kubernetes-ingress-controller
Submodule kubernetes-ingress-controller deleted from 58579a
16 changes: 0 additions & 16 deletions scripts/verify-submodules-status.sh

This file was deleted.

Loading

0 comments on commit 28e3810

Please sign in to comment.