...

Source file src/edge-infra.dev/pkg/edge/flux/bootstrap/config.go

Documentation: edge-infra.dev/pkg/edge/flux/bootstrap

     1  package bootstrap
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	kustomizeApiV1 "github.com/fluxcd/kustomize-controller/api/v1"
     8  	kustomizeApiV1beta2 "github.com/fluxcd/kustomize-controller/api/v1beta2"
     9  	meta2 "github.com/fluxcd/pkg/apis/meta"
    10  	sourceApiV1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"sigs.k8s.io/controller-runtime/pkg/client"
    13  )
    14  
    15  const (
    16  	// bucket
    17  	gcpEndpoint = "storage.googleapis.com"
    18  	gcpProvider = "gcp"
    19  
    20  	// kustomize
    21  	kindBucket = "Bucket"
    22  )
    23  
    24  var (
    25  	// both
    26  	defaultTimeout  = 5 * time.Minute
    27  	defaultInterval = 60 * time.Second
    28  
    29  	// bucket
    30  	defaultInsecure = false
    31  
    32  	// kustomize
    33  	defaultForce = true
    34  	defaultPrune = true
    35  )
    36  
    37  type FluxBucket struct {
    38  	name       string
    39  	namespace  string
    40  	interval   time.Duration
    41  	secretName string
    42  	timeout    time.Duration
    43  	ignore     string
    44  	provider   string
    45  	endpoint   string
    46  	bucketName string
    47  	insecure   bool
    48  }
    49  
    50  func BucketFluxConfig() *FluxBucket {
    51  	return &FluxBucket{timeout: defaultTimeout, interval: defaultInterval, provider: gcpProvider,
    52  		endpoint: gcpEndpoint, insecure: defaultInsecure}
    53  }
    54  
    55  func (b *FluxBucket) Name(name string) *FluxBucket {
    56  	b.name = name
    57  	return b
    58  }
    59  
    60  func (b *FluxBucket) Namespace(ns string) *FluxBucket {
    61  	b.namespace = ns
    62  	return b
    63  }
    64  
    65  func (b *FluxBucket) Interval(interval time.Duration) *FluxBucket {
    66  	b.interval = interval
    67  	return b
    68  }
    69  
    70  func (b *FluxBucket) SecretName(secretName string) *FluxBucket {
    71  	b.secretName = secretName
    72  	return b
    73  }
    74  
    75  func (b *FluxBucket) Timeout(timeout time.Duration) *FluxBucket {
    76  	b.timeout = timeout
    77  	return b
    78  }
    79  
    80  func (b *FluxBucket) ForCluster(cluster string) *FluxBucket {
    81  	b.ignore = fmt.Sprintf("# exclude all\n/*\n# include deploy dir\n!/%s", cluster)
    82  	return b
    83  }
    84  
    85  func (b *FluxBucket) BucketName(bn string) *FluxBucket {
    86  	b.bucketName = bn
    87  	return b
    88  }
    89  
    90  func (b *FluxBucket) Build() client.Object {
    91  	retV1Beta2 := &sourceApiV1beta2.Bucket{
    92  		TypeMeta: metav1.TypeMeta{
    93  			Kind:       sourceApiV1beta2.BucketKind,
    94  			APIVersion: sourceApiV1beta2.GroupVersion.String(),
    95  		},
    96  		ObjectMeta: metav1.ObjectMeta{
    97  			Name:      b.name,
    98  			Namespace: b.namespace,
    99  		},
   100  		Spec: sourceApiV1beta2.BucketSpec{
   101  			Interval: metav1.Duration{
   102  				Duration: b.interval,
   103  			},
   104  			Provider:   b.provider,
   105  			BucketName: b.bucketName,
   106  			Endpoint:   b.endpoint,
   107  			Insecure:   b.insecure,
   108  			Timeout: &metav1.Duration{
   109  				Duration: b.timeout,
   110  			},
   111  			Ignore: &b.ignore,
   112  		},
   113  	}
   114  	if b.secretName != "" {
   115  		retV1Beta2.Spec.SecretRef = &meta2.LocalObjectReference{
   116  			Name: b.secretName,
   117  		}
   118  	}
   119  	return retV1Beta2
   120  }
   121  
   122  type FluxKustomize struct {
   123  	name            string
   124  	namespace       string
   125  	interval        time.Duration
   126  	path            string
   127  	timeout         time.Duration
   128  	bucketName      string
   129  	bucketNamespace string
   130  	kind            string
   131  	force           bool
   132  	prune           bool
   133  	annotations     map[string]string
   134  	clusterVersion  string
   135  }
   136  
   137  func KustomizeFluxConfig() *FluxKustomize {
   138  	return &FluxKustomize{timeout: defaultTimeout, interval: defaultInterval,
   139  		kind: kindBucket, force: defaultForce, prune: defaultPrune}
   140  }
   141  
   142  func (k *FluxKustomize) Name(name string) *FluxKustomize {
   143  	k.name = name
   144  	return k
   145  }
   146  
   147  func (k *FluxKustomize) Namespace(ns string) *FluxKustomize {
   148  	k.namespace = ns
   149  	return k
   150  }
   151  
   152  func (k *FluxKustomize) Interval(interval time.Duration) *FluxKustomize {
   153  	k.interval = interval
   154  	return k
   155  }
   156  
   157  func (k *FluxKustomize) Path(path string) *FluxKustomize {
   158  	k.path = path
   159  	return k
   160  }
   161  
   162  func (k *FluxKustomize) Force(force bool) *FluxKustomize {
   163  	k.force = force
   164  	return k
   165  }
   166  
   167  func (k *FluxKustomize) Timeout(timeout time.Duration) *FluxKustomize {
   168  	k.timeout = timeout
   169  	return k
   170  }
   171  
   172  func (k *FluxKustomize) BucketName(bn string) *FluxKustomize {
   173  	k.bucketName = bn
   174  	return k
   175  }
   176  
   177  func (k *FluxKustomize) BucketNamespace(ns string) *FluxKustomize {
   178  	k.bucketNamespace = ns
   179  	return k
   180  }
   181  
   182  func (k *FluxKustomize) Prune(p bool) *FluxKustomize {
   183  	k.prune = p
   184  	return k
   185  }
   186  
   187  func (k *FluxKustomize) Annotations(annotations map[string]string) *FluxKustomize {
   188  	k.annotations = annotations
   189  	return k
   190  }
   191  
   192  func (k *FluxKustomize) ForStoreVersion(version string) *FluxKustomize {
   193  	k.clusterVersion = version
   194  	return k
   195  }
   196  
   197  func (k *FluxKustomize) Build() client.Object {
   198  	var res client.Object
   199  	supports := SupportsFluxV024(k.clusterVersion)
   200  	if !supports {
   201  		res = &kustomizeApiV1beta2.Kustomization{
   202  			TypeMeta: metav1.TypeMeta{
   203  				Kind:       kustomizeApiV1beta2.KustomizationKind,
   204  				APIVersion: kustomizeApiV1beta2.GroupVersion.String(),
   205  			},
   206  			ObjectMeta: metav1.ObjectMeta{
   207  				Name:        k.name,
   208  				Namespace:   k.namespace,
   209  				Annotations: k.annotations,
   210  			},
   211  
   212  			Spec: kustomizeApiV1beta2.KustomizationSpec{
   213  				Interval: metav1.Duration{
   214  					Duration: k.interval,
   215  				},
   216  				Force: k.force,
   217  				Path:  k.path,
   218  				Prune: k.prune,
   219  				SourceRef: kustomizeApiV1beta2.CrossNamespaceSourceReference{
   220  					APIVersion: sourceApiV1beta2.GroupVersion.String(),
   221  					Kind:       k.kind,
   222  					Name:       k.bucketName,
   223  					Namespace:  k.bucketNamespace,
   224  				},
   225  				Timeout: &metav1.Duration{
   226  					Duration: k.timeout,
   227  				},
   228  			},
   229  		}
   230  	} else {
   231  		res = &kustomizeApiV1.Kustomization{
   232  			TypeMeta: metav1.TypeMeta{
   233  				Kind:       kustomizeApiV1.KustomizationKind,
   234  				APIVersion: kustomizeApiV1.GroupVersion.String(),
   235  			},
   236  			ObjectMeta: metav1.ObjectMeta{
   237  				Name:        k.name,
   238  				Namespace:   k.namespace,
   239  				Annotations: k.annotations,
   240  			},
   241  
   242  			Spec: kustomizeApiV1.KustomizationSpec{
   243  				Interval: metav1.Duration{
   244  					Duration: k.interval,
   245  				},
   246  				Force: k.force,
   247  				Path:  k.path,
   248  				Prune: k.prune,
   249  				SourceRef: kustomizeApiV1.CrossNamespaceSourceReference{
   250  					APIVersion: sourceApiV1beta2.GroupVersion.String(),
   251  					Kind:       k.kind,
   252  					Name:       k.bucketName,
   253  					Namespace:  k.bucketNamespace,
   254  				},
   255  				Timeout: &metav1.Duration{
   256  					Duration: k.timeout,
   257  				},
   258  			},
   259  		}
   260  	}
   261  	return res
   262  }
   263  

View as plain text