Skip to content

Commit

Permalink
refactor: kmp spec interval as string
Browse files Browse the repository at this point in the history
  • Loading branch information
duffney committed Jul 17, 2024
1 parent 3790882 commit ca14514
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
4 changes: 2 additions & 2 deletions api/v1beta1/keymanagementproviders_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type KeyManagementProviderSpec struct {
// Name of the key management provider
Type string `json:"type,omitempty"`

// +kubebuilder:default=1
Interval int32 `json:"interval,omitempty"`
// +kubebuilder:default="1m"
Interval string `json:"interval,omitempty"`

// +kubebuilder:pruning:PreserveUnknownFields
// Parameters of the key management provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ spec:
description: KeyManagementProviderSpec defines the desired state of KeyManagementProvider
properties:
interval:
default: 1
format: int32
type: integer
default: 1m
type: string
parameters:
description: Parameters of the key management provider
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

_ "github.com/ratify-project/ratify/pkg/keymanagementprovider/azurekeyvault" // register azure key vault key management provider
_ "github.com/ratify-project/ratify/pkg/keymanagementprovider/inline" // register inline key management provider
"github.com/ratify-project/ratify/pkg/keymanagementprovider/refresh" // register inline key management provider
"github.com/ratify-project/ratify/pkg/keymanagementprovider/refresh"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down
26 changes: 7 additions & 19 deletions pkg/keymanagementprovider/refresh/kubeRefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,19 @@ func (kr *KubeRefresher) Refresh(ctx context.Context) error {
}

// resource is refreshable, requeue after interval
intervalDuration := time.Duration(keyManagementProvider.Spec.Interval) * time.Minute
intervalDuration, err := time.ParseDuration(keyManagementProvider.Spec.Interval)
if err != nil {
logger.Error(err, "unable to parse interval duration")
kr.Result = ctrl.Result{}
return err

Check warning on line 127 in pkg/keymanagementprovider/refresh/kubeRefresh.go

View check run for this annotation

Codecov / codecov/patch

pkg/keymanagementprovider/refresh/kubeRefresh.go#L123-L127

Added lines #L123 - L127 were not covered by tests
}

logger.Info("Reconciled KeyManagementProvider", "intervalDuration", intervalDuration)
kr.Result = ctrl.Result{RequeueAfter: intervalDuration}

Check warning on line 131 in pkg/keymanagementprovider/refresh/kubeRefresh.go

View check run for this annotation

Codecov / codecov/patch

pkg/keymanagementprovider/refresh/kubeRefresh.go#L130-L131

Added lines #L130 - L131 were not covered by tests

return nil

Check warning on line 133 in pkg/keymanagementprovider/refresh/kubeRefresh.go

View check run for this annotation

Codecov / codecov/patch

pkg/keymanagementprovider/refresh/kubeRefresh.go#L133

Added line #L133 was not covered by tests
}

/*
reconcile method logic
"path/to/your/project/refresher"
lastRefresh, err := c.Refresher.Refresh()
func (r *KeyManagementProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
refreshResult := r.KubeRefresher.Refresh(ctx)
if refreshResult.Error != nil {
// Handle error
return ctrl.Result{}, refreshResult.Error
}
// Continue with reconcile logic using refreshResult.Result if necessary
return refreshResult.Result, nil
}
*/

func writeKMProviderStatus(ctx context.Context, r client.StatusClient, keyManagementProvider *configv1beta1.KeyManagementProvider, logger *logrus.Entry, isSuccess bool, errorString string, operationTime metav1.Time, kmProviderStatus kmp.KeyManagementProviderStatus) {
if isSuccess {
updateKMProviderSuccessStatus(keyManagementProvider, &operationTime, kmProviderStatus)
Expand Down
42 changes: 27 additions & 15 deletions pkg/keymanagementprovider/refresh/kubeRefresh_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package refresh
/*
Copyright The Ratify 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
//TODO blank import of provider for intis
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 refresh

import (
"context"
Expand All @@ -21,20 +35,20 @@ import (

func TestKubeRefresher_Refresh(t *testing.T) {
tests := []struct {
name string
name string
provider *configv1beta1.KeyManagementProvider
request ctrl.Request
wantErr bool
request ctrl.Request
wantErr bool
}{
{
name: "valid params",
provider: &configv1beta1.KeyManagementProvider{
ObjectMeta: metav1.ObjectMeta{
Namespace: "" ,
Namespace: "",
Name: "kmpName",
},
Spec: configv1beta1.KeyManagementProviderSpec{
Type: "inline",
Type: "inline",
Parameters: runtime.RawExtension{
Raw: []byte(`{"type": "inline", "contentType": "certificate", "value": "-----BEGIN CERTIFICATE-----\nMIID2jCCAsKgAwIBAgIQXy2VqtlhSkiZKAGhsnkjbDANBgkqhkiG9w0BAQsFADBvMRswGQYDVQQD\nExJyYXRpZnkuZXhhbXBsZS5jb20xDzANBgNVBAsTBk15IE9yZzETMBEGA1UEChMKTXkgQ29tcGFu\neTEQMA4GA1UEBxMHUmVkbW9uZDELMAkGA1UECBMCV0ExCzAJBgNVBAYTAlVTMB4XDTIzMDIwMTIy\nNDUwMFoXDTI0MDIwMTIyNTUwMFowbzEbMBkGA1UEAxMScmF0aWZ5LmV4YW1wbGUuY29tMQ8wDQYD\nVQQLEwZNeSBPcmcxEzARBgNVBAoTCk15IENvbXBhbnkxEDAOBgNVBAcTB1JlZG1vbmQxCzAJBgNV\nBAgTAldBMQswCQYDVQQGEwJVUzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL10bM81\npPAyuraORABsOGS8M76Bi7Guwa3JlM1g2D8CuzSfSTaaT6apy9GsccxUvXd5cmiP1ffna5z+EFmc\nizFQh2aq9kWKWXDvKFXzpQuhyqD1HeVlRlF+V0AfZPvGt3VwUUjNycoUU44ctCWmcUQP/KShZev3\n6SOsJ9q7KLjxxQLsUc4mg55eZUThu8mGB8jugtjsnLUYvIWfHhyjVpGrGVrdkDMoMn+u33scOmrt\nsBljvq9WVo4T/VrTDuiOYlAJFMUae2Ptvo0go8XTN3OjLblKeiK4C+jMn9Dk33oGIT9pmX0vrDJV\nX56w/2SejC1AxCPchHaMuhlwMpftBGkCAwEAAaNyMHAwDgYDVR0PAQH/BAQDAgeAMAkGA1UdEwQC\nMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwMwHwYDVR0jBBgwFoAU0eaKkZj+MS9jCp9Dg1zdv3v/aKww\nHQYDVR0OBBYEFNHmipGY/jEvYwqfQ4Nc3b97/2isMA0GCSqGSIb3DQEBCwUAA4IBAQBNDcmSBizF\nmpJlD8EgNcUCy5tz7W3+AAhEbA3vsHP4D/UyV3UgcESx+L+Nye5uDYtTVm3lQejs3erN2BjW+ds+\nXFnpU/pVimd0aYv6mJfOieRILBF4XFomjhrJOLI55oVwLN/AgX6kuC3CJY2NMyJKlTao9oZgpHhs\nLlxB/r0n9JnUoN0Gq93oc1+OLFjPI7gNuPXYOP1N46oKgEmAEmNkP1etFrEjFRgsdIFHksrmlOlD\nIed9RcQ087VLjmuymLgqMTFX34Q3j7XgN2ENwBSnkHotE9CcuGRW+NuiOeJalL8DBmFXXWwHTKLQ\nPp5g6m1yZXylLJaFLKz7tdMmO355\n-----END CERTIFICATE-----\n"}`),
},
Expand All @@ -52,16 +66,16 @@ func TestKubeRefresher_Refresh(t *testing.T) {
name: "nonexistent KMP",
provider: &configv1beta1.KeyManagementProvider{
ObjectMeta: metav1.ObjectMeta{
Namespace: "" ,
Namespace: "",
Name: "kmpName",
},
Spec: configv1beta1.KeyManagementProviderSpec{
Type: "inline",
Type: "inline",
},
},
request: ctrl.Request{
NamespacedName: client.ObjectKey{
Name: "nonexistent",
Name: "nonexistent",
},
},
wantErr: false,
Expand All @@ -70,11 +84,11 @@ func TestKubeRefresher_Refresh(t *testing.T) {
name: "invalid params",
provider: &configv1beta1.KeyManagementProvider{
ObjectMeta: metav1.ObjectMeta{
Namespace: "" ,
Namespace: "",
Name: "kmpName",
},
Spec: configv1beta1.KeyManagementProviderSpec{
Type: "inline",
Type: "inline",
},
},
request: ctrl.Request{
Expand All @@ -101,7 +115,6 @@ func TestKubeRefresher_Refresh(t *testing.T) {
}
}


func TestKMProviderUpdateErrorStatus(t *testing.T) {
var parametersString = "{\"certs\":{\"name\":\"certName\"}}"
var kmProviderStatus = []byte(parametersString)
Expand Down Expand Up @@ -137,7 +150,6 @@ func TestKMProviderUpdateErrorStatus(t *testing.T) {
}
}


// TestKMProviderUpdateSuccessStatus tests the updateSuccessStatus method
func TestKMProviderUpdateSuccessStatus(t *testing.T) {
kmProviderStatus := keymanagementprovider.KeyManagementProviderStatus{}
Expand Down Expand Up @@ -247,4 +259,4 @@ func TestWriteKMProviderStatus(t *testing.T) {
}
})
}
}
}

0 comments on commit ca14514

Please sign in to comment.