mirror of https://github.com/fluxcd/flux2.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
6.5 KiB
Go
228 lines
6.5 KiB
Go
4 years ago
|
/*
|
||
4 years ago
|
Copyright 2020 The Flux authors
|
||
4 years ago
|
|
||
|
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 main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
|
||
|
"github.com/spf13/cobra"
|
||
|
corev1 "k8s.io/api/core/v1"
|
||
|
"k8s.io/apimachinery/pkg/api/errors"
|
||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||
|
"k8s.io/apimachinery/pkg/types"
|
||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||
|
|
||
4 years ago
|
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
|
||
4 years ago
|
"github.com/fluxcd/toolkit/internal/flags"
|
||
4 years ago
|
"github.com/fluxcd/toolkit/internal/utils"
|
||
4 years ago
|
)
|
||
|
|
||
|
var createSourceBucketCmd = &cobra.Command{
|
||
|
Use: "bucket [name]",
|
||
|
Short: "Create or update a Bucket source",
|
||
|
Long: `
|
||
|
The create source bucket command generates a Bucket resource and waits for it to be downloaded.
|
||
|
For Buckets with static authentication, the credentials are stored in a Kubernetes secret.`,
|
||
|
Example: ` # Create a source from a Buckets using static authentication
|
||
4 years ago
|
flux create source bucket podinfo \
|
||
4 years ago
|
--bucket-name=podinfo \
|
||
|
--endpoint=minio.minio.svc.cluster.local:9000 \
|
||
|
--insecure=true \
|
||
|
--access-key=myaccesskey \
|
||
|
--secret-key=mysecretkey \
|
||
|
--interval=10m
|
||
|
|
||
|
# Create a source from an Amazon S3 Bucket using IAM authentication
|
||
4 years ago
|
flux create source bucket podinfo \
|
||
4 years ago
|
--bucket-name=podinfo \
|
||
|
--provider=aws \
|
||
|
--endpoint=s3.amazonaws.com \
|
||
|
--region=us-east-1 \
|
||
|
--interval=10m
|
||
|
`,
|
||
|
RunE: createSourceBucketCmdRun,
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
sourceBucketName string
|
||
4 years ago
|
sourceBucketProvider = flags.SourceBucketProvider(sourcev1.GenericBucketProvider)
|
||
4 years ago
|
sourceBucketEndpoint string
|
||
|
sourceBucketAccessKey string
|
||
|
sourceBucketSecretKey string
|
||
|
sourceBucketRegion string
|
||
|
sourceBucketInsecure bool
|
||
4 years ago
|
sourceBucketSecretRef string
|
||
4 years ago
|
)
|
||
|
|
||
|
func init() {
|
||
4 years ago
|
createSourceBucketCmd.Flags().Var(&sourceBucketProvider, "provider", sourceBucketProvider.Description())
|
||
4 years ago
|
createSourceBucketCmd.Flags().StringVar(&sourceBucketName, "bucket-name", "", "the bucket name")
|
||
|
createSourceBucketCmd.Flags().StringVar(&sourceBucketEndpoint, "endpoint", "", "the bucket endpoint address")
|
||
|
createSourceBucketCmd.Flags().StringVar(&sourceBucketAccessKey, "access-key", "", "the bucket access key")
|
||
|
createSourceBucketCmd.Flags().StringVar(&sourceBucketSecretKey, "secret-key", "", "the bucket secret key")
|
||
|
createSourceBucketCmd.Flags().StringVar(&sourceBucketRegion, "region", "", "the bucket region")
|
||
|
createSourceBucketCmd.Flags().BoolVar(&sourceBucketInsecure, "insecure", false, "for when connecting to a non-TLS S3 HTTP endpoint")
|
||
4 years ago
|
createSourceBucketCmd.Flags().StringVar(&sourceBucketSecretRef, "secret-ref", "", "the name of an existing secret containing credentials")
|
||
4 years ago
|
|
||
|
createSourceCmd.AddCommand(createSourceBucketCmd)
|
||
|
}
|
||
|
|
||
|
func createSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
|
||
|
if len(args) < 1 {
|
||
4 years ago
|
return fmt.Errorf("Bucket source name is required")
|
||
4 years ago
|
}
|
||
|
name := args[0]
|
||
|
|
||
|
if sourceBucketName == "" {
|
||
|
return fmt.Errorf("bucket-name is required")
|
||
|
}
|
||
|
|
||
|
if sourceBucketEndpoint == "" {
|
||
|
return fmt.Errorf("endpoint is required")
|
||
|
}
|
||
|
|
||
|
sourceLabels, err := parseLabels()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
tmpDir, err := ioutil.TempDir("", name)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer os.RemoveAll(tmpDir)
|
||
|
|
||
4 years ago
|
bucket := &sourcev1.Bucket{
|
||
4 years ago
|
ObjectMeta: metav1.ObjectMeta{
|
||
|
Name: name,
|
||
|
Namespace: namespace,
|
||
|
Labels: sourceLabels,
|
||
|
},
|
||
|
Spec: sourcev1.BucketSpec{
|
||
|
BucketName: sourceBucketName,
|
||
4 years ago
|
Provider: sourceBucketProvider.String(),
|
||
4 years ago
|
Insecure: sourceBucketInsecure,
|
||
|
Endpoint: sourceBucketEndpoint,
|
||
|
Region: sourceBucketRegion,
|
||
|
Interval: metav1.Duration{
|
||
|
Duration: interval,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
4 years ago
|
if sourceHelmSecretRef != "" {
|
||
|
bucket.Spec.SecretRef = &corev1.LocalObjectReference{
|
||
|
Name: sourceBucketSecretRef,
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
if export {
|
||
4 years ago
|
return exportBucket(*bucket)
|
||
4 years ago
|
}
|
||
|
|
||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||
|
defer cancel()
|
||
|
|
||
4 years ago
|
kubeClient, err := utils.KubeClient(kubeconfig)
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
4 years ago
|
logger.Generatef("generating Bucket source")
|
||
4 years ago
|
|
||
4 years ago
|
if sourceBucketSecretRef == "" {
|
||
|
secretName := fmt.Sprintf("bucket-%s", name)
|
||
4 years ago
|
|
||
4 years ago
|
secret := corev1.Secret{
|
||
|
ObjectMeta: metav1.ObjectMeta{
|
||
|
Name: secretName,
|
||
|
Namespace: namespace,
|
||
|
},
|
||
|
StringData: map[string]string{},
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
if sourceBucketAccessKey != "" && sourceBucketSecretKey != "" {
|
||
|
secret.StringData["accesskey"] = sourceBucketAccessKey
|
||
|
secret.StringData["secretkey"] = sourceBucketSecretKey
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
if len(secret.StringData) > 0 {
|
||
|
logger.Actionf("applying secret with the bucket credentials")
|
||
|
if err := upsertSecret(ctx, kubeClient, secret); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
bucket.Spec.SecretRef = &corev1.LocalObjectReference{
|
||
|
Name: secretName,
|
||
|
}
|
||
|
logger.Successf("authentication configured")
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
logger.Actionf("applying Bucket source")
|
||
|
namespacedName, err := upsertBucket(ctx, kubeClient, bucket)
|
||
|
if err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
|
|
||
4 years ago
|
logger.Waitingf("waiting for Bucket source reconciliation")
|
||
4 years ago
|
if err := wait.PollImmediate(pollInterval, timeout,
|
||
4 years ago
|
isBucketReady(ctx, kubeClient, namespacedName, bucket)); err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
4 years ago
|
logger.Successf("Bucket source reconciliation completed")
|
||
4 years ago
|
|
||
4 years ago
|
if bucket.Status.Artifact == nil {
|
||
|
return fmt.Errorf("Bucket source reconciliation but no artifact was found")
|
||
4 years ago
|
}
|
||
4 years ago
|
logger.Successf("fetched revision: %s", bucket.Status.Artifact.Revision)
|
||
4 years ago
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
func upsertBucket(ctx context.Context, kubeClient client.Client,
|
||
|
bucket *sourcev1.Bucket) (types.NamespacedName, error) {
|
||
4 years ago
|
namespacedName := types.NamespacedName{
|
||
|
Namespace: bucket.GetNamespace(),
|
||
|
Name: bucket.GetName(),
|
||
|
}
|
||
|
|
||
|
var existing sourcev1.Bucket
|
||
|
err := kubeClient.Get(ctx, namespacedName, &existing)
|
||
|
if err != nil {
|
||
|
if errors.IsNotFound(err) {
|
||
4 years ago
|
if err := kubeClient.Create(ctx, bucket); err != nil {
|
||
|
return namespacedName, err
|
||
4 years ago
|
} else {
|
||
4 years ago
|
logger.Successf("Bucket source created")
|
||
|
return namespacedName, nil
|
||
4 years ago
|
}
|
||
|
}
|
||
4 years ago
|
return namespacedName, err
|
||
4 years ago
|
}
|
||
|
|
||
|
existing.Labels = bucket.Labels
|
||
|
existing.Spec = bucket.Spec
|
||
|
if err := kubeClient.Update(ctx, &existing); err != nil {
|
||
4 years ago
|
return namespacedName, err
|
||
4 years ago
|
}
|
||
4 years ago
|
bucket = &existing
|
||
|
logger.Successf("Bucket source updated")
|
||
|
return namespacedName, nil
|
||
4 years ago
|
}
|