Skip to content

Commit

Permalink
Embed CRD files using Go's embed library, remove old auto-gen (#9297)
Browse files Browse the repository at this point in the history
* Use Go's embed library to embed the CRD YAMLs.

* calicoctl: use embedded CRDs, remove old autogeneration

Bye bye crds.go.  Your long lines won't be missed.

* Extra test for AllCRDs().
  • Loading branch information
fasaxc authored Oct 2, 2024
1 parent 1600ac9 commit d7d7be4
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 265 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ generate:
$(MAKE) -C api gen-files
$(MAKE) -C libcalico-go gen-files
$(MAKE) -C felix gen-files
$(MAKE) -C calicoctl gen-crds
$(MAKE) -C app-policy protobuf
$(MAKE) gen-manifests

Expand Down
6 changes: 0 additions & 6 deletions calicoctl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ bin/calicoctl: bin/calicoctl-linux-amd64
bin/calicoctl-windows-amd64.exe: bin/calicoctl-windows-amd64
mv $< $@

gen-crds:
$(DOCKER_RUN) \
$(CALICO_BUILD) \
sh -c 'cd /go/src/$(PACKAGE_NAME)/calicoctl/commands/crds && go generate'
$(MAKE) go-fmt

###############################################################################
# Building the image
###############################################################################
Expand Down
39 changes: 0 additions & 39 deletions calicoctl/calicoctl/commands/crds/crds.go

This file was deleted.

148 changes: 0 additions & 148 deletions calicoctl/calicoctl/commands/crds/decode.go

This file was deleted.

9 changes: 4 additions & 5 deletions calicoctl/calicoctl/commands/datastore/migrate/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ import (
"strings"

"github.com/docopt/docopt-go"
apiv3 "github.com/projectcalico/api/pkg/apis/projectcalico/v3"
yaml "github.com/projectcalico/go-yaml-wrapper"
log "github.com/sirupsen/logrus"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

apiv3 "github.com/projectcalico/api/pkg/apis/projectcalico/v3"
yaml "github.com/projectcalico/go-yaml-wrapper"

"github.com/projectcalico/calico/calicoctl/calicoctl/commands/clientmgr"
"github.com/projectcalico/calico/calicoctl/calicoctl/commands/common"
"github.com/projectcalico/calico/calicoctl/calicoctl/commands/constants"
"github.com/projectcalico/calico/calicoctl/calicoctl/commands/crds"
"github.com/projectcalico/calico/calicoctl/calicoctl/util"
lcconfig "github.com/projectcalico/calico/libcalico-go/config"
"github.com/projectcalico/calico/libcalico-go/lib/apiconfig"
libapiv3 "github.com/projectcalico/calico/libcalico-go/lib/apis/v3"
"github.com/projectcalico/calico/libcalico-go/lib/backend/k8s"
Expand Down Expand Up @@ -377,7 +376,7 @@ func importCRDs(cfg *apiconfig.CalicoAPIConfig) error {
log.Debugf("Created k8s CRD ClientSet: %+v", cs)

// Apply the CRDs
calicoCRDs, err := crds.CalicoCRDs()
calicoCRDs, err := lcconfig.AllCRDs()
if err != nil {
return err
}
Expand Down
65 changes: 0 additions & 65 deletions calicoctl/scripts/importcrds.go

This file was deleted.

2 changes: 1 addition & 1 deletion libcalico-go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ gen-files: gen-crds

## Force a rebuild of custom resource definition yamls
gen-crds:
rm -rf config
rm -rf config/crds
$(DOCKER_GO_BUILD) sh -c '$(GIT_CONFIG_SSH) controller-gen crd:crdVersions=v1 paths=./lib/apis/... output:crd:dir=config/crd/'
@rm config/crd/_.yaml
# Patch in manual tweaks to the generated CRDs.
Expand Down
70 changes: 70 additions & 0 deletions libcalico-go/config/crds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2024 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"embed"
"fmt"
"strings"

v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/yaml"
)

// CRDFiles is a filesystem that contains the CRD YAML definitions.
//
// CRDs are stored in the FS under the crd/ directory. For example,
// the CRD for the FelixConfiguration resource is stored in the file
// crd/crd.projectcalico.org_felixconfigurations.yaml.
//
//go:embed crd/*.yaml
var CRDFiles embed.FS

func LoadCRD(group, name string) (*v1.CustomResourceDefinition, error) {
rawYAML, err := CRDFiles.ReadFile("crd/" + group + "_" + name + ".yaml")
if err != nil {
return nil, fmt.Errorf("failed to load CRD YAML from embedded FS: %w", err)
}
var crd v1.CustomResourceDefinition
err = yaml.Unmarshal(rawYAML, &crd)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal CRD YAML: %w", err)
}
return &crd, nil
}

func AllCRDs() ([]*v1.CustomResourceDefinition, error) {
var crds []*v1.CustomResourceDefinition
entries, err := CRDFiles.ReadDir("crd")
if err != nil {
return nil, fmt.Errorf("failed to read CRD directory: %w", err)
}
for _, d := range entries {
if d.IsDir() || !strings.HasSuffix(d.Name(), ".yaml") {
continue
}
rawYAML, err := CRDFiles.ReadFile("crd/" + d.Name())
if err != nil {
return nil, fmt.Errorf("failed to load CRD YAML from embedded FS: %w", err)
}
var crd v1.CustomResourceDefinition
err = yaml.Unmarshal(rawYAML, &crd)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal CRD YAML: %w", err)
}
crds = append(crds, &crd)
}
return crds, nil
}
Loading

0 comments on commit d7d7be4

Please sign in to comment.