Skip to content

Commit

Permalink
feat: implementation of catalog polling feature
Browse files Browse the repository at this point in the history
  • Loading branch information
exdx committed Dec 17, 2019
1 parent 2864699 commit be97ada
Show file tree
Hide file tree
Showing 11 changed files with 419 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ IMAGE_TAG ?= "dev"
SPECIFIC_UNIT_TEST := $(if $(TEST),-run $(TEST),)
LOCAL_NAMESPACE := "olm"
export GO111MODULE=on
CONTROLLER_GEN := go run ./vendor/sigs.k8s.io/controller-tools/cmd/controller-gen
CONTROLLER_GEN := go run -mod=vendor ./vendor/sigs.k8s.io/controller-tools/cmd/controller-gen
YQ_INTERNAL := go run ./vendor/github.com/mikefarah/yq/v2/

# ART builds are performed in dist-git, with content (but not commits) copied
Expand Down Expand Up @@ -158,9 +158,12 @@ codegen:
go run vendor/k8s.io/kube-openapi/cmd/openapi-gen/openapi-gen.go --logtostderr -i ./vendor/k8s.io/apimachinery/pkg/runtime,./vendor/k8s.io/apimachinery/pkg/apis/meta/v1,./vendor/k8s.io/apimachinery/pkg/version,./pkg/package-server/apis/operators/v1,./pkg/package-server/apis/apps/v1alpha1,./pkg/api/apis/operators/v1alpha1,./pkg/lib/version -p $(PKG)/pkg/package-server/apis/openapi -O zz_generated.openapi -h boilerplate.go.txt -r /dev/null
$(CODEGEN_INTERNAL) deepcopy,conversion,client,lister,informer $(PKG)/pkg/api/client $(PKG)/pkg/api/apis $(PKG)/pkg/api/apis "operators:v1alpha1,v1"
$(CODEGEN_INTERNAL) all $(PKG)/pkg/package-server/client $(PKG)/pkg/package-server/apis $(PKG)/pkg/package-server/apis "operators:v1 apps:v1alpha1"

manifestgen:
$(CONTROLLER_GEN) schemapatch:manifests=./deploy/chart/templates paths=./pkg/api/apis/operators/v1alpha1/... output:dir=./deploy/chart/templates
$(CONTROLLER_GEN) schemapatch:manifests=./deploy/chart/templates paths=./pkg/api/apis/operators/v1/... output:dir=./deploy/chart/templates
$(YQ_INTERNAL) w --inplace deploy/chart/templates/0000_50_olm_03-clusterserviceversion.crd.yaml spec.validation.openAPIV3Schema.properties.spec.properties.install.properties.spec.properties.deployments.items.properties.spec.properties.template.properties.metadata.x-kubernetes-preserve-unknown-fields true

container-codegen:
docker build -t olm:codegen -f codegen.Dockerfile .
docker run --name temp-codegen olm:codegen /bin/true
Expand Down
10 changes: 10 additions & 0 deletions deploy/chart/templates/0000_50_olm_06-catalogsource.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ spec:
type: string
description:
type: string
poll:
type: object
description: Poll represents the duration between successive checks of the catalog image to ensure the
latest version of the catalogsource is present.
required:
- interval
properties:
interval:
type: string
description: The real time interval between polls of the catalog image registry.
displayName:
description: Metadata
type: string
Expand Down
21 changes: 16 additions & 5 deletions pkg/api/apis/operators/catalogsource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ type CatalogSourceSpec struct {
// +Optional
Image string

// Poll is used to determine the time interval between checks of the latest catalog source version.
// The catalog operator polls to see if a new version of the catalog source is available.
// If available, the latest image is pulled and gRPC traffic is directed to the latest catalog source.
// +Optional
Poll Poll

// Secrets represent set of secrets that can be used to access the contents of the catalog.
// It is best to keep this list small, since each will need to be tried for every catalog entry.
// +Optional
Expand All @@ -59,6 +65,10 @@ type CatalogSourceSpec struct {
Icon Icon
}

type Poll struct {
Interval metav1.Duration
}

type RegistryServiceStatus struct {
Protocol string
ServiceName string
Expand All @@ -78,11 +88,12 @@ func (s *RegistryServiceStatus) Address() string {
}

type CatalogSourceStatus struct {
Message string `json:"message,omitempty"`
Reason ConditionReason `json:"reason,omitempty"`
ConfigMapResource *ConfigMapResourceReference
RegistryServiceStatus *RegistryServiceStatus
GRPCConnectionState *GRPCConnectionState
Message string `json:"message,omitempty"`
Reason ConditionReason `json:"reason,omitempty"`
ConfigMapResource *ConfigMapResourceReference
RegistryServiceStatus *RegistryServiceStatus
GRPCConnectionState *GRPCConnectionState
LatestImageRegistryPoll metav1.Time
}

type ConfigMapResourceReference struct {
Expand Down
45 changes: 45 additions & 0 deletions pkg/api/apis/operators/v1alpha1/catalogsource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1alpha1

import (
"fmt"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -54,6 +55,12 @@ type CatalogSourceSpec struct {
// +Optional
Image string `json:"image,omitempty"`

// Poll is used to determine the time interval between checks of the latest catalog source version.
// The catalog operator polls to see if a new version of the catalog source is available.
// If available, the latest image is pulled and gRPC traffic is directed to the latest catalog source.
// +Optional
Poll Poll `json:"poll,omitempty"`

// Secrets represent set of secrets that can be used to access the contents of the catalog.
// It is best to keep this list small, since each will need to be tried for every catalog entry.
// +Optional
Expand All @@ -66,6 +73,10 @@ type CatalogSourceSpec struct {
Icon Icon `json:"icon,omitempty"`
}

type Poll struct {
Interval metav1.Duration `json:"interval,omitempty"`
}

type RegistryServiceStatus struct {
Protocol string `json:"protocol,omitempty"`
ServiceName string `json:"serviceName,omitempty"`
Expand All @@ -92,6 +103,9 @@ type CatalogSourceStatus struct {
// +optional
Reason ConditionReason `json:"reason,omitempty"`

// The last time the CatalogSource image registry has been polled to ensure the image is up-to-date
LatestImageRegistryPoll metav1.Time `json:"lastUpdateTime,omitempty"`

ConfigMapResource *ConfigMapResourceReference `json:"configMapReference,omitempty"`
RegistryServiceStatus *RegistryServiceStatus `json:"registryService,omitempty"`
GRPCConnectionState *GRPCConnectionState `json:"connectionState,omitempty"`
Expand Down Expand Up @@ -137,6 +151,37 @@ func (c *CatalogSource) SetError(reason ConditionReason, err error) {
}
}

func (c *CatalogSource) SetLastUpdateTime() {
c.Status.LatestImageRegistryPoll = metav1.Now()
}

// Check if it is time to update based on polling setting
func (c *CatalogSource) ReadyToUpdate() bool {
interval := c.Spec.Poll.Interval.Duration

// if polling interval is zero polling will not be done
if interval == time.Duration(0) {
return false
}

// if catalog source is not backed by an image polling will not be done
if c.Spec.Image == "" {
return false
}

if c.Status.LatestImageRegistryPoll.IsZero() {
if c.CreationTimestamp.Add(interval).Before(time.Now()) {
return true
}
} else {
if c.Status.LatestImageRegistryPoll.Add(interval).Before(time.Now()) {
return true
}
}

return false
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// CatalogSourceList is a repository of CSVs, CRDs, and operator packages.
Expand Down
62 changes: 62 additions & 0 deletions pkg/api/apis/operators/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package v1alpha1
import (
"sort"
"testing"
"time"

"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetAllCRDDescriptions(t *testing.T) {
Expand Down Expand Up @@ -102,3 +104,63 @@ func TestOwnsCRD(t *testing.T) {
require.Equal(t, tt.expected, csv.OwnsCRD(tt.crdName))
}
}

func TestCatalogSource_ReadyToUpdate(t *testing.T) {
var table = []struct {
description string
catsrc CatalogSource
result bool
sleep time.Duration
}{
{
description: "poll interval set to zero: do not check for updates",
catsrc: CatalogSource{Spec: CatalogSourceSpec{Poll: Poll{Interval: metav1.Duration{}}}},
result: false,
},
{
description: "not image based catalog source: do not check for updates",
catsrc: CatalogSource{Spec: CatalogSourceSpec{Poll: Poll{Interval: metav1.Duration{Duration: 1 * time.Second}},
Address: "127.0.0.1:8080"}},
result: false,
},
{
description: "polling interval set: last update time zero: update for the first time",
catsrc: CatalogSource{
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Time{Time: time.Now()}},
Spec: CatalogSourceSpec{
Poll: Poll{Interval: metav1.Duration{Duration: 1 * time.Second}},
Image: "mycatsrcimage"}},
result: true,
sleep: 2 * time.Second,
},
{
description: "polling interval set: time to update based on previous poll timestamp",
catsrc: CatalogSource{
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Time{Time: time.Now()}},
Spec: CatalogSourceSpec{
Poll: Poll{Interval: metav1.Duration{Duration: 1 * time.Second}},
Image: "mycatsrcimage"},
Status: CatalogSourceStatus{LatestImageRegistryPoll: metav1.Time{Time: time.Now()}},
},
result: true,
sleep: 2 * time.Second,
},
{
description: "polling interval set: not time to update based on previous poll timestamp",
catsrc: CatalogSource{
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Time{Time: time.Now()}},
Spec: CatalogSourceSpec{
Poll: Poll{Interval: metav1.Duration{Duration: 1 * time.Second}},
Image: "mycatsrcimage"},
Status: CatalogSourceStatus{LatestImageRegistryPoll: metav1.Time{Time: time.Now()}},
},
result: true,
sleep: 2 * time.Second,
},
}

for i, tt := range table {
time.Sleep(table[i].sleep)
require.Equal(t, tt.result, table[i].catsrc.ReadyToUpdate(), table[i].description)
}
}
62 changes: 60 additions & 2 deletions pkg/api/apis/operators/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/api/apis/operators/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be97ada

Please sign in to comment.