Skip to content

Commit

Permalink
RemoteRootSyncSet: use specified OCI image (kptdev#2722)
Browse files Browse the repository at this point in the history
Stop hard-coding and use the image the user specifies!
  • Loading branch information
justinsb authored and martinmaly committed Feb 18, 2022
1 parent 86c62b7 commit 0b33036
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,26 @@ type RemoteRootSyncSet struct {
Status RemoteRootSyncSetStatus `json:"status,omitempty"`
}

func (o *RemoteRootSyncSet) GetSpec() *RemoteRootSyncSetSpec {
if o == nil {
return nil
}
return &o.Spec
}

// RemoteRootSyncSetSpec defines the desired state of RemoteRootSync
type RemoteRootSyncSetSpec struct {
ClusterRefs []*ClusterRef `json:"clusterRefs,omitempty"`
Template *RootSyncTemplate `json:"template,omitempty"`
}

func (o *RemoteRootSyncSetSpec) GetTemplate() *RootSyncTemplate {
if o == nil {
return nil
}
return o.Template
}

type ClusterRef struct {
ApiVersion string `json:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty"`
Expand All @@ -61,10 +75,24 @@ type RootSyncTemplate struct {
OCI *OCISpec `json:"oci,omitempty"`
}

func (o *RootSyncTemplate) GetOCI() *OCISpec {
if o == nil {
return nil
}
return o.OCI
}

type OCISpec struct {
Repository string `json:"repository,omitempty"`
}

func (o *OCISpec) GetRepository() string {
if o == nil {
return ""
}
return o.Repository
}

// RootSyncSetStatus defines the observed state of RootSyncSet
type RemoteRootSyncSetStatus struct {
Targets []TargetStatus `json:"targets,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,17 @@ func (r *RemoteRootSyncSetReconciler) applyToClusterRef(ctx context.Context, sub

// BuildObjectsToApply config root sync
func (r *RemoteRootSyncSetReconciler) BuildObjectsToApply(ctx context.Context, subject *api.RemoteRootSyncSet) ([]*unstructured.Unstructured, error) {
// TODO: stop hard-coding the image source; get from a deployment instead
gcpProjectID := os.Getenv("GCP_PROJECT_ID")
imageName := oci.ImageTagName{
Image: "us-west1-docker.pkg.dev/" + gcpProjectID + "/deployment/myfirstnginx",
Tag: "v1",
repository := subject.GetSpec().GetTemplate().GetOCI().GetRepository()
if repository == "" {
return nil, fmt.Errorf("spec.template.oci.repository is not set")
}
imageName, err := oci.ParseImageTagName(repository)
if err != nil {
return nil, fmt.Errorf("unable to parse image %q: %w", repository, err)
}
klog.Infof("image name %s -> %#v", repository, *imageName)

digest, err := r.ociStorage.LookupImageTag(ctx, imageName)
digest, err := r.ociStorage.LookupImageTag(ctx, *imageName)
if err != nil {
return nil, err
}
Expand Down
11 changes: 11 additions & 0 deletions porch/repository/pkg/oci/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ func (i ImageTagName) ociReference() (name.Reference, error) {
return imageRef, nil
}

func ParseImageTagName(s string) (*ImageTagName, error) {
t, err := name.NewTag(s)
if err != nil {
return nil, fmt.Errorf("cannot parse %q as tag: %w", s, err)
}
return &ImageTagName{
Image: t.Repository.Name(),
Tag: t.TagStr(),
}, nil
}

// ImageDigestName holds an image we know by digest (which is immutable and more cacheable)
type ImageDigestName struct {
Image string
Expand Down

0 comments on commit 0b33036

Please sign in to comment.