-
Notifications
You must be signed in to change notification settings - Fork 245
Add target/deploy/dry-run CLI commands #2102
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
762ba8b
New CLI commands to create and deploy bundledeployments
1ced38e
Apply suggestions from code review
f6650d1
Add validation around input file
a23b071
rename --list-inputs to --dump-input-list
af5703b
Update internal/cmd/cli/deploy.go
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
kind: Bundle | ||
apiVersion: fleet.cattle.io/v1alpha1 | ||
metadata: | ||
name: match-all-clusters | ||
namespace: fleet-local | ||
spec: | ||
resources: | ||
- content: | | ||
kind: ConfigMap | ||
apiVersion: v1 | ||
metadata: | ||
name: root | ||
data: | ||
value: root | ||
name: cm.yaml | ||
targets: | ||
- clusterSelector: {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
kind: Bundle | ||
apiVersion: fleet.cattle.io/v1alpha1 | ||
metadata: | ||
name: external-secret | ||
namespace: fleet-local | ||
spec: | ||
resources: | ||
- content: | | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-deployment | ||
labels: | ||
app: nginx | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 | ||
name: nginx.yaml | ||
targets: | ||
- clusterName: local |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
apiVersion: fleet.cattle.io/v1alpha1 | ||
kind: BundleDeployment | ||
metadata: | ||
creationTimestamp: null | ||
labels: | ||
fleet.cattle.io/bundle-name: testbundle-simple-chart | ||
fleet.cattle.io/bundle-namespace: fleet-local | ||
fleet.cattle.io/cluster: local | ||
fleet.cattle.io/cluster-namespace: fleet-local | ||
fleet.cattle.io/commit: e40edabfeada51874ac9caf5770655b720177380 | ||
fleet.cattle.io/managed: "true" | ||
name: testbundle-simple-chart | ||
namespace: cluster-fleet-local-local-1a3d67d0a899 | ||
spec: | ||
deploymentID: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e:c32e813ecbf48f56833aa2267cd3d8758eecc94f9948fb0dea510147a57760b5 | ||
options: | ||
helm: | ||
chart: config-chart | ||
values: | ||
name: example-value | ||
ignore: {} | ||
stagedDeploymentID: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e:c32e813ecbf48f56833aa2267cd3d8758eecc94f9948fb0dea510147a57760b5 | ||
stagedOptions: | ||
helm: | ||
chart: config-chart | ||
values: | ||
name: example-value | ||
ignore: {} | ||
status: | ||
display: {} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
apiVersion: fleet.cattle.io/v1alpha1 | ||
content: H4sIAAAAAAAA/4SRMW/bMBCF9/6KA2eLtlygA7fCa7t6KTucpZNNhKII8iTEEPTfA5KRESdOsgjQ3Xv33gfOIlAcxtBQFOrfLBz2JJRoBteZc9VcMPD2kL7yir0Vm7RhciyUQG+OFKIZnIJpr12yKnjr1K6l2ATjOYt+A1NkyCvgCzJMFExnKILh+OrUjq+eFKD31jSYnNpNa85O1nKnHXp/i9ailvUvudNCO7FsHiMw9d4iU9yWcY/+G6BauyfjWgWHbPiLXrueGFtkVNoBFNxEVEXTe0slqloxbsIkSTUntCPV+5+p6GqfZ5DHNI8yDWBZvoCYivBD73KqpQ5Hy1X6uz/SWaIH73ch2+eC+fr7lwMocVmx1qVnzKR5dR/yWbkxWAVnO5zQylKksWNkCn/wRLZgp0v/lx8vAQAA//+su6S1jAIAAA== | ||
kind: Content | ||
metadata: | ||
creationTimestamp: null | ||
name: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e | ||
sha256sum: ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36eebc | ||
|
||
--- | ||
apiVersion: fleet.cattle.io/v1alpha1 | ||
kind: BundleDeployment | ||
metadata: | ||
creationTimestamp: null | ||
labels: | ||
fleet.cattle.io/bundle-name: testbundle-simple-chart | ||
fleet.cattle.io/bundle-namespace: fleet-local | ||
fleet.cattle.io/cluster: local | ||
fleet.cattle.io/cluster-namespace: fleet-local | ||
fleet.cattle.io/commit: e40edabfeada51874ac9caf5770655b720177380 | ||
fleet.cattle.io/managed: "true" | ||
name: testbundle-simple-chart | ||
namespace: cluster-fleet-local-local-1a3d67d0a899 | ||
spec: | ||
deploymentID: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e:c32e813ecbf48f56833aa2267cd3d8758eecc94f9948fb0dea510147a57760b5 | ||
options: | ||
helm: | ||
chart: config-chart | ||
values: | ||
name: example-value | ||
ignore: {} | ||
stagedDeploymentID: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e:c32e813ecbf48f56833aa2267cd3d8758eecc94f9948fb0dea510147a57760b5 | ||
stagedOptions: | ||
helm: | ||
chart: config-chart | ||
values: | ||
name: example-value | ||
ignore: {} | ||
status: | ||
display: {} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
apiVersion: fleet.cattle.io/v1alpha1 | ||
content: H4sIAAAAAAAA/4SRMW/bMBCF9/6KA2eLtlygA7fCa7t6KTucpZNNhKII8iTEEPTfA5KRESdOsgjQ3Xv33gfOIlAcxtBQFOrfLBz2JJRoBteZc9VcMPD2kL7yir0Vm7RhciyUQG+OFKIZnIJpr12yKnjr1K6l2ATjOYt+A1NkyCvgCzJMFExnKILh+OrUjq+eFKD31jSYnNpNa85O1nKnHXp/i9ailvUvudNCO7FsHiMw9d4iU9yWcY/+G6BauyfjWgWHbPiLXrueGFtkVNoBFNxEVEXTe0slqloxbsIkSTUntCPV+5+p6GqfZ5DHNI8yDWBZvoCYivBD73KqpQ5Hy1X6uz/SWaIH73ch2+eC+fr7lwMocVmx1qVnzKR5dR/yWbkxWAVnO5zQylKksWNkCn/wRLZgp0v/lx8vAQAA//+su6S1jAIAAA== | ||
kind: Content | ||
metadata: | ||
creationTimestamp: null | ||
name: s-ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36e | ||
sha256sum: ee0480cffe0c0da150814f6844d7c5bc49cc05c158c5ba1efe9a45142f36eebc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package deploy_test | ||
|
||
import ( | ||
"github.com/onsi/gomega/gbytes" | ||
|
||
clihelper "github.com/rancher/fleet/integrationtests/cli" | ||
"github.com/rancher/fleet/integrationtests/utils" | ||
"github.com/rancher/fleet/internal/cmd/cli" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Fleet CLI Deploy", func() { | ||
var args []string | ||
|
||
act := func(args []string) (*gbytes.Buffer, error) { | ||
cmd := cli.NewDeploy() | ||
args = append([]string{"--kubeconfig", kubeconfigPath}, args...) | ||
cmd.SetArgs(args) | ||
|
||
buf := gbytes.NewBuffer() | ||
cmd.SetOutput(buf) | ||
err := cmd.Execute() | ||
return buf, err | ||
} | ||
|
||
BeforeEach(func() { | ||
var err error | ||
namespace, err = utils.NewNamespaceName() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(k8sClient.Create(ctx, &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespace, | ||
}, | ||
})).ToNot(HaveOccurred()) | ||
|
||
DeferCleanup(func() { | ||
Expect(k8sClient.Delete(ctx, &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespace, | ||
}, | ||
})).ToNot(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
When("input file parameter is missing", func() { | ||
It("prints the help", func() { | ||
buf, err := act(args) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf).To(gbytes.Say("Usage:")) | ||
}) | ||
}) | ||
|
||
When("Input file is missing", func() { | ||
BeforeEach(func() { | ||
args = []string{"--input-file", "/tmp/does-not-exist-bundle.yaml"} | ||
}) | ||
|
||
It("prints an error", func() { | ||
errBuf, err := act(args) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(errBuf).To(gbytes.Say("no such file or directory")) | ||
}) | ||
}) | ||
|
||
When("Input file is invalid", func() { | ||
BeforeEach(func() { | ||
args = []string{"--input-file", clihelper.AssetsPath + "helmrepository/config-chart-0.1.0.tgz"} | ||
}) | ||
|
||
It("prints an error", func() { | ||
errBuf, err := act(args) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(errBuf).To(gbytes.Say("yaml: control characters are not allowed")) | ||
}) | ||
}) | ||
|
||
When("Input file does not contain a content resource", func() { | ||
BeforeEach(func() { | ||
args = []string{"--input-file", clihelper.AssetsPath + "bundledeployment/bd-only.yaml"} | ||
}) | ||
|
||
It("prints an error", func() { | ||
errBuf, err := act(args) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(errBuf).To(gbytes.Say("failed to read content resource from file")) | ||
}) | ||
}) | ||
|
||
When("Input file does not contain a bundledeployment resource", func() { | ||
BeforeEach(func() { | ||
args = []string{"--input-file", clihelper.AssetsPath + "bundledeployment/content.yaml"} | ||
}) | ||
|
||
It("prints an error", func() { | ||
errBuf, err := act(args) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(errBuf).To(gbytes.Say("failed to read bundledeployment")) | ||
}) | ||
}) | ||
|
||
When("Deploying to a cluster", func() { | ||
BeforeEach(func() { | ||
args = []string{ | ||
"--input-file", clihelper.AssetsPath + "bundledeployment/bd.yaml", | ||
} | ||
}) | ||
|
||
It("creates resources", func() { | ||
buf, err := act(args) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf).To(gbytes.Say("defaultNamespace: default")) | ||
Expect(buf).To(gbytes.Say("objects")) | ||
Expect(buf).To(gbytes.Say("- apiVersion: v1")) | ||
|
||
cm := &corev1.ConfigMap{} | ||
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "test-simple-chart-config"}, cm) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
When("Specifying a namespace", func() { | ||
BeforeEach(func() { | ||
args = []string{ | ||
"--input-file", clihelper.AssetsPath + "bundledeployment/bd.yaml", | ||
"--namespace", namespace, | ||
} | ||
}) | ||
|
||
It("creates resources", func() { | ||
buf, err := act(args) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf).To(gbytes.Say("defaultNamespace: " + namespace)) | ||
Expect(buf).To(gbytes.Say("objects")) | ||
Expect(buf).To(gbytes.Say("- apiVersion: v1")) | ||
|
||
cm := &corev1.ConfigMap{} | ||
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "test-simple-chart-config"}, cm) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
When("Printing results with --dry-run", func() { | ||
BeforeEach(func() { | ||
args = []string{ | ||
"--input-file", clihelper.AssetsPath + "bundledeployment/bd.yaml", | ||
"--dry-run", | ||
} | ||
}) | ||
|
||
It("prints a manifest and bundledeployment", func() { | ||
buf, err := act(args) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(buf).To(gbytes.Say("- apiVersion: v1")) | ||
Expect(buf).To(gbytes.Say(" data:")) | ||
Expect(buf).To(gbytes.Say(" name: example-value")) | ||
|
||
cm := &corev1.ConfigMap{} | ||
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "test-simple-chart-config"}, cm) | ||
Expect(apierrors.IsNotFound(err)).To(BeTrue()) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package deploy_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/rancher/fleet/integrationtests/utils" | ||
"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
) | ||
|
||
var ( | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
cfg *rest.Config | ||
testEnv *envtest.Environment | ||
tmpdir string | ||
kubeconfigPath string | ||
|
||
k8sClient client.Client | ||
namespace string | ||
|
||
scheme = runtime.NewScheme() | ||
) | ||
|
||
func TestFleetDeploy(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Fleet CLI Deploy Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
ctx, cancel = context.WithCancel(context.TODO()) | ||
testEnv = &envtest.Environment{ | ||
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "charts", "fleet-crd", "templates", "crds.yaml")}, | ||
ErrorIfCRDPathMissing: true, | ||
} | ||
|
||
var err error | ||
cfg, err = testEnv.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(cfg).NotTo(BeNil()) | ||
|
||
tmpdir, _ = os.MkdirTemp("", "fleet-") | ||
kubeconfigPath = path.Join(tmpdir, "kubeconfig") | ||
err = utils.WriteKubeConfig(cfg, kubeconfigPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// scheme for k8sClient | ||
utilruntime.Must(clientgoscheme.AddToScheme(scheme)) | ||
utilruntime.Must(v1alpha1.AddToScheme(scheme)) | ||
|
||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(k8sClient).NotTo(BeNil()) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
if os.Getenv("SKIP_CLEANUP") == "true" { | ||
return | ||
} | ||
os.RemoveAll(tmpdir) | ||
|
||
cancel() | ||
_ = testEnv.Stop() | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.