|
| 1 | +//go:build mage |
| 2 | +// +build mage |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | + |
| 11 | + "github.com/magefile/mage/mg" |
| 12 | + "github.com/magefile/mage/sh" |
| 13 | +) |
| 14 | + |
| 15 | +// Dependency Versions |
| 16 | +const ( |
| 17 | + controllerGenVersion = "0.6.2" |
| 18 | + kindVersion = "0.20.0" |
| 19 | + yqVersion = "4.35.1" |
| 20 | + goimportsVersion = "0.12.0" |
| 21 | + golangciLintVersion = "1.54.2" |
| 22 | + olmVersion = "0.20.0" |
| 23 | + opmVersion = "1.24.0" |
| 24 | + pkoCliVersion = "1.6.1" |
| 25 | + helmVersion = "3.12.2" |
| 26 | +) |
| 27 | + |
| 28 | +type Dependency mg.Namespace |
| 29 | + |
| 30 | +func (d Dependency) All() { |
| 31 | + mg.Deps( |
| 32 | + Dependency.Kind, |
| 33 | + Dependency.ControllerGen, |
| 34 | + Dependency.YQ, |
| 35 | + Dependency.Goimports, |
| 36 | + Dependency.GolangciLint, |
| 37 | + Dependency.Helm, |
| 38 | + Dependency.Opm, |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +// Ensure Kind dependency - Kubernetes in Docker (or Podman) |
| 43 | +func (d Dependency) Kind() error { |
| 44 | + return depsDir.GoInstall("kind", |
| 45 | + "sigs.k8s.io/kind", kindVersion) |
| 46 | +} |
| 47 | + |
| 48 | +// Ensure controller-gen - kubebuilder code and manifest generator. |
| 49 | +func (d Dependency) ControllerGen() error { |
| 50 | + return depsDir.GoInstall("controller-gen", |
| 51 | + "sigs.k8s.io/controller-tools/cmd/controller-gen", controllerGenVersion) |
| 52 | +} |
| 53 | + |
| 54 | +// Ensure yq - jq but for Yaml, written in Go. |
| 55 | +func (d Dependency) YQ() error { |
| 56 | + return depsDir.GoInstall("yq", |
| 57 | + "github.com/mikefarah/yq/v4", yqVersion) |
| 58 | +} |
| 59 | + |
| 60 | +func (d Dependency) Goimports() error { |
| 61 | + return depsDir.GoInstall("go-imports", |
| 62 | + "golang.org/x/tools/cmd/goimports", goimportsVersion) |
| 63 | +} |
| 64 | + |
| 65 | +func (d Dependency) GolangciLint() error { |
| 66 | + return depsDir.GoInstall("golangci-lint", |
| 67 | + "github.com/golangci/golangci-lint/cmd/golangci-lint", golangciLintVersion) |
| 68 | +} |
| 69 | + |
| 70 | +func (d Dependency) Helm() error { |
| 71 | + return depsDir.GoInstall("helm", "helm.sh/helm/v3/cmd/helm", helmVersion) |
| 72 | +} |
| 73 | + |
| 74 | +func (d Dependency) Opm() error { |
| 75 | + // TODO: move this into devkube library, to ensure the depsDir is present, even if you just call "NeedsRebuild" |
| 76 | + if err := os.MkdirAll(depsDir.Bin(), os.ModePerm); err != nil { |
| 77 | + return fmt.Errorf("create dependency dir: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + needsRebuild, err := depsDir.NeedsRebuild("opm", opmVersion) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + if !needsRebuild { |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + // Tempdir |
| 89 | + tempDir, err := os.MkdirTemp(cacheDir, "") |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("temp dir: %w", err) |
| 92 | + } |
| 93 | + defer os.RemoveAll(tempDir) |
| 94 | + |
| 95 | + // Download |
| 96 | + tempOPMBin := path.Join(tempDir, "opm") |
| 97 | + if err := sh.RunV( |
| 98 | + "curl", "-L", "--fail", |
| 99 | + "-o", tempOPMBin, |
| 100 | + fmt.Sprintf( |
| 101 | + "https://github.com/operator-framework/operator-registry/releases/download/v%s/linux-amd64-opm", |
| 102 | + opmVersion, |
| 103 | + ), |
| 104 | + ); err != nil { |
| 105 | + return fmt.Errorf("downloading opm: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + if err := os.Chmod(tempOPMBin, 0755); err != nil { |
| 109 | + return fmt.Errorf("make opm executable: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Move |
| 113 | + if err := os.Rename(tempOPMBin, path.Join(depsDir.Bin(), "opm")); err != nil { |
| 114 | + return fmt.Errorf("move opm: %w", err) |
| 115 | + } |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func (d Dependency) PkoCli() error { |
| 120 | + if err := os.MkdirAll(depsDir.Bin(), os.ModePerm); err != nil { |
| 121 | + return fmt.Errorf("create dependency dir: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + needsRebuild, err := depsDir.NeedsRebuild("kubectl-package", pkoCliVersion) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + if !needsRebuild { |
| 129 | + return nil |
| 130 | + } |
| 131 | + |
| 132 | + // Tempdir |
| 133 | + tempDir, err := os.MkdirTemp(cacheDir, "") |
| 134 | + if err != nil { |
| 135 | + return fmt.Errorf("temp dir: %w", err) |
| 136 | + } |
| 137 | + defer os.RemoveAll(tempDir) |
| 138 | + |
| 139 | + // Download |
| 140 | + tempPkoCliBin := path.Join(tempDir, "kubectl-package") |
| 141 | + if err := sh.RunV( |
| 142 | + "curl", "-L", "--fail", |
| 143 | + "-o", tempPkoCliBin, |
| 144 | + fmt.Sprintf( |
| 145 | + "https://github.com/package-operator/package-operator/releases/download/v%s/kubectl-package_linux_amd64", |
| 146 | + pkoCliVersion, |
| 147 | + ), |
| 148 | + ); err != nil { |
| 149 | + return fmt.Errorf("downloading kubectl-package: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + if err := os.Chmod(tempPkoCliBin, 0755); err != nil { |
| 153 | + return fmt.Errorf("make kubectl-package executable: %w", err) |
| 154 | + } |
| 155 | + |
| 156 | + // Move |
| 157 | + if err := os.Rename(tempPkoCliBin, path.Join(depsDir.Bin(), "kubectl-package")); err != nil { |
| 158 | + return fmt.Errorf("move kubectl-package: %w", err) |
| 159 | + } |
| 160 | + return nil |
| 161 | +} |
0 commit comments