Skip to content

Commit

Permalink
renamed symbols and added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
droot committed May 30, 2023
1 parent ca2ea2c commit 74d3dcc
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 37 deletions.
1 change: 1 addition & 0 deletions rollouts/api/v1alpha1/remotesync_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type GitInfo struct {
}

// Oci contains configuration specific to importing resources from an OCI package.
// This definition is copied from https://github.com/GoogleContainerTools/kpt-config-sync/blob/main/pkg/api/configsync/v1beta1/ociconfig.go
type OciInfo struct {
// image is the OCI image repository URL for the package to sync from.
// e.g. `LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY_NAME/PACKAGE_NAME`.
Expand Down
4 changes: 4 additions & 0 deletions rollouts/api/v1alpha1/rollout_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ type PackageSourceType string
type PackagesConfig struct {
SourceType PackageSourceType `json:"sourceType"`

// TODO(droot): Change Github and Gitlab to pointers because
// One of the the following will be non-nil to follow OneOf semantics.
GitHub GitHubSource `json:"github,omitempty"`
GitLab GitLabSource `json:"gitlab,omitempty"`
OciSource *OCISource `json:"oci,omitempty"`
Expand Down Expand Up @@ -159,6 +161,7 @@ type GitLabSelector struct {
Branch string `json:"branch,omitempty"`
}

// OCISource defines configuration to discover OCI packages.
type OCISource struct {
// image is the OCI image repository URL for the package to sync from.
// e.g. `LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY_NAME/PACKAGE_NAME`.
Expand All @@ -171,6 +174,7 @@ type OCISource struct {

// dir is the absolute path of the directory that contains
// the local resources. Default: the root directory of the image.
// Note (droot): We will extend `Dir` to express variants of a package at some point.
// +optional
Dir string `json:"dir,omitempty"`
}
Expand Down
3 changes: 2 additions & 1 deletion rollouts/config/crd/bases/gitops.kpt.dev_remotesyncs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ spec:
type: object
oci:
description: Oci contains configuration specific to importing
resources from an OCI package.
resources from an OCI package. This definition is copied
from https://github.com/GoogleContainerTools/kpt-config-sync/blob/main/pkg/api/configsync/v1beta1/ociconfig.go
properties:
auth:
description: auth is the type of secret configured for
Expand Down
14 changes: 10 additions & 4 deletions rollouts/config/crd/bases/gitops.kpt.dev_rollouts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ spec:
description: Packages source for this Rollout.
properties:
github:
description: GitHubSource defines the packages source in GitHub.
description: 'TODO(droot): Change Github and Gitlab to pointers
because One of the the following will be non-nil to follow OneOf
semantics.'
properties:
selector:
description: GitHubSelector defines the selector to apply
Expand Down Expand Up @@ -168,11 +170,13 @@ spec:
- selector
type: object
oci:
description: OCISource defines configuration to discover OCI packages.
properties:
dir:
description: 'dir is the absolute path of the directory that
contains the local resources. Default: the root directory
of the image.'
of the image. Note (droot): We will extend `Dir` to express
variants of a package at some point.'
type: string
image:
description: 'image is the OCI image repository URL for the
Expand Down Expand Up @@ -299,7 +303,8 @@ spec:
type: object
oci:
description: Oci contains configuration specific to importing
resources from an OCI package.
resources from an OCI package. This definition is copied
from https://github.com/GoogleContainerTools/kpt-config-sync/blob/main/pkg/api/configsync/v1beta1/ociconfig.go
properties:
auth:
description: auth is the type of secret configured for
Expand Down Expand Up @@ -396,7 +401,8 @@ spec:
type: object
oci:
description: Oci contains configuration specific to importing
resources from an OCI package.
resources from an OCI package. This definition is copied
from https://github.com/GoogleContainerTools/kpt-config-sync/blob/main/pkg/api/configsync/v1beta1/ociconfig.go
properties:
auth:
description: auth is the type of secret configured for
Expand Down
22 changes: 19 additions & 3 deletions rollouts/config/samples/test-ns-rollout.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# Copyright 2023 The kpt Authors
#
# 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.

# An example Rollout that deploys an OCI package
# to the target clusters.
apiVersion: gitops.kpt.dev/v1alpha1
kind: Rollout
metadata:
Expand All @@ -8,11 +24,11 @@ spec:
sourceType: GCPFleet
gcpFleet:
projectIds:
- sunilarora-sandbox
- <GCP_FLEET_PROJECT_ID>
packages:
sourceType: OCI
oci:
image: us-west1-docker.pkg.dev/sunilarora-sandbox/rollouts-samples/test-namespace:v1
image: us-west1-docker.pkg.dev/GCP_PROJECT_ID/oci-artifact:v1
dir: .
packageToTargetMatcher:
type: AllClusters
Expand All @@ -27,7 +43,7 @@ spec:
oci:
image: something
auth: gcpserviceaccount
gcpServiceAccountEmail: rollouts-samples-reader@sunilarora-sandbox.iam.gserviceaccount.com
gcpServiceAccountEmail: GCP_SA@GCP_PROJECTID.iam.gserviceaccount.com
strategy:
type: RollingUpdate
rollingUpdate:
Expand Down
22 changes: 20 additions & 2 deletions rollouts/controllers/remotesync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
"sync"

apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -194,7 +195,7 @@ func (r *RemoteSyncReconciler) syncExternalSync(ctx context.Context, rs *gitopsv
return "", fmt.Errorf("failed to create/update sync: %w", err)
}

r.setupWatches(ctx, externalSyncName(rs), rs.Namespace, rs.Spec.ClusterRef)
r.setupWatches(ctx, getExternalSyncName(rs), rs.Namespace, rs.Spec.ClusterRef)

syncStatus, err := checkSyncStatus(ctx, dynCl, rs)
if err != nil {
Expand Down Expand Up @@ -348,7 +349,7 @@ func BuildObjectsToApply(remotesync *gitopsv1alpha1.RemoteSync,

u := unstructured.Unstructured{Object: newRootSync}
u.SetGroupVersionKind(gvk)
u.SetName(externalSyncName(remotesync))
u.SetName(getExternalSyncName(remotesync))
u.SetNamespace(namespace)

labels := u.GetLabels()
Expand Down Expand Up @@ -418,6 +419,23 @@ func getExternalSyncNamespace(rs *gitopsv1alpha1.RemoteSync) string {
}
}

// makeRemoteSyncName constructs the name of the RemoteSync object
// by prefixing rolloutName with clusterName.
// For example, RemoteSync object's name for a rollout `app-rollout` and
// target cluster `gke-1` will be `gke-1-app-rollout`.
func makeRemoteSyncName(clusterName, rolloutName string) string {
return fmt.Sprintf("%s-%s", clusterName, rolloutName)
}

// getExternalSyncName returns the name of the RSync object's name.
// It is derived by stripping away the cluster-name prefix
// from the RemoteSync object's name. We use rollout-name as RSync object's name.
func getExternalSyncName(rrs *gitopsv1alpha1.RemoteSync) string {
clusterRef := rrs.Spec.ClusterRef
clusterName := clusterRef.Name[strings.LastIndex(clusterRef.Name, "/")+1:]
return strings.TrimPrefix(rrs.GetName(), fmt.Sprintf("%s-", clusterName))
}

// SetupWithManager sets up the controller with the Manager.
func (r *RemoteSyncReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.channel = make(chan event.GenericEvent, 10)
Expand Down
29 changes: 7 additions & 22 deletions rollouts/controllers/rollout_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,7 @@ func (r *RolloutReconciler) computeTargets(ctx context.Context,
rs := gitopsv1alpha1.RemoteSync{}
key := client.ObjectKey{
Namespace: rollout.Namespace,
// Name: fmt.Sprintf("%s-%s", pkgID(pkg), clusterName),
Name: remoteSyncName(clusterName, rollout.GetName()),
Name: makeRemoteSyncName(clusterName, rollout.GetName()),
}
// since this RS need to exist, remove it from the deletion list
delete(RSkeysToBeDeleted, key)
Expand Down Expand Up @@ -456,16 +455,6 @@ func (r *RolloutReconciler) computeTargets(ctx context.Context,
return targets, nil
}

func remoteSyncName(clusterName, rolloutName string) string {
return fmt.Sprintf("%s-%s", clusterName, rolloutName)
}

func externalSyncName(rrs *gitopsv1alpha1.RemoteSync) string {
clusterRef := rrs.Spec.ClusterRef
clusterName := clusterRef.Name[strings.LastIndex(clusterRef.Name, "/")+1:]
return strings.TrimPrefix(rrs.GetName(), fmt.Sprintf("%s-", clusterName))
}

// rsNeedsUpdate checks if the underlying remotesync needs to be updated by creating a new RemoteSync object and comparing it to the existing one
func rsNeedsUpdate(ctx context.Context, rollout *gitopsv1alpha1.Rollout, currentRS *gitopsv1alpha1.RemoteSync, target *clusterPackagePair) (*gitopsv1alpha1.RemoteSync, bool) {
desiredRS := newRemoteSync(rollout, target)
Expand Down Expand Up @@ -743,8 +732,7 @@ func newRemoteSync(rollout *gitopsv1alpha1.Rollout, target *clusterPackagePair)
// or a RootSync in the config-management-system namespace.
return &gitopsv1alpha1.RemoteSync{
ObjectMeta: metav1.ObjectMeta{
// Name: fmt.Sprintf("%s-%s", pkgID(target.packageRef), clusterName),
Name: remoteSyncName(clusterName, rollout.GetName()),
Name: makeRemoteSyncName(clusterName, rollout.GetName()),
Namespace: rollout.Namespace,
Labels: map[string]string{
rolloutLabel: rollout.Name,
Expand Down Expand Up @@ -779,16 +767,17 @@ func toSyncSpec(dpkg *packagediscovery.DiscoveredPackage, rollout *gitopsv1alpha
case dpkg.OciRepo != nil:
syncSpec.SourceType = "oci"
syncSpec.Oci = &gitopsv1alpha1.OciInfo{
// TODO(droot): Repo URL can be an HTTP, GIT or SSH based URL
// Need to make it configurable
Image: dpkg.OciRepo.Image,
Dir: dpkg.Directory,
}
if rollout.Spec.SyncTemplate.RootSync != nil {
// copy the fields from the RSync template
if rollout.Spec.SyncTemplate.RepoSync != nil {
syncSpec.Oci.Auth = rollout.Spec.SyncTemplate.RepoSync.Oci.Auth
syncSpec.Oci.GCPServiceAccountEmail = rollout.Spec.SyncTemplate.RepoSync.Oci.GCPServiceAccountEmail
} else {
syncSpec.Oci.Auth = rollout.Spec.SyncTemplate.RootSync.Oci.Auth
syncSpec.Oci.GCPServiceAccountEmail = rollout.Spec.SyncTemplate.RootSync.Oci.GCPServiceAccountEmail
}
// TODO(droot): support reposync as well
default:
syncSpec.SourceType = "git"
syncSpec.Git = &gitopsv1alpha1.GitInfo{
Expand All @@ -804,10 +793,6 @@ func toSyncSpec(dpkg *packagediscovery.DiscoveredPackage, rollout *gitopsv1alpha
return syncSpec
}

func pkgID(dpkg *packagediscovery.DiscoveredPackage) string {
return dpkg.ID()
}

// SetupWithManager sets up the controller with the Manager.
func (r *RolloutReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := gkeclusterapis.AddToScheme(mgr.GetScheme()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion rollouts/controllers/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func checkSyncStatus(ctx context.Context, client dynamic.Interface, remotesync *
return "", err
}

rs, err := client.Resource(gvr).Namespace(getExternalSyncNamespace(remotesync)).Get(ctx, externalSyncName(remotesync), metav1.GetOptions{})
rs, err := client.Resource(gvr).Namespace(getExternalSyncNamespace(remotesync)).Get(ctx, getExternalSyncName(remotesync), metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get %s: %w", gvk.Kind, err)
}
Expand Down
3 changes: 2 additions & 1 deletion rollouts/pkg/packagediscovery/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
)

// getOCIPackages discovers OCI packages for OCI config.
// TODO(droot): Support variants discovery in the future.
func (d *PackageDiscovery) getOCIPackages(ctx context.Context, config gitopsv1alpha1.PackagesConfig) ([]DiscoveredPackage, error) {
var discoveredPackages []DiscoveredPackage

oci := config.OciSource

discoveredPackages = append(discoveredPackages, DiscoveredPackage{
Directory: oci.Dir,
OciRepo: &OCIArtifact{
OciRepo: &OCIRepo{
Image: oci.Image,
},
})
Expand Down
6 changes: 3 additions & 3 deletions rollouts/pkg/packagediscovery/packagediscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ type DiscoveredPackage struct {
GitLabProject *gitlab.Project
// GithubRepo contains the info retrieved from GitHub
GitHubRepo *github.Repository

OciRepo *OCIArtifact
// OciRepo contains info retrieved from the OCI registry
OciRepo *OCIRepo
}

type OCIArtifact struct {
type OCIRepo struct {
Image string
}

Expand Down

0 comments on commit 74d3dcc

Please sign in to comment.