Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
Checksum reconciled Instances and Bindings (#582)
Browse files Browse the repository at this point in the history
* Add checksum field to InstanceSpec

* Add checksum to Binding

* Regenerate code

Regenerate code

more generation
  • Loading branch information
pmorie authored and Ville Aikas committed Mar 20, 2017
1 parent 1dfd38b commit c2a9dfb
Show file tree
Hide file tree
Showing 20 changed files with 1,906 additions and 1,137 deletions.
66 changes: 66 additions & 0 deletions pkg/apis/servicecatalog/checksum/checksum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2017 The Kubernetes 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.
*/

package checksum

import (
"testing"

"k8s.io/kubernetes/pkg/api/v1"

"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/checksum/unversioned"
checksumv1alpha1 "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/checksum/versioned/v1alpha1"
_ "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/install"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1alpha1"
)

func TestInstanceSpecChecksum(t *testing.T) {
spec := servicecatalog.InstanceSpec{
ServiceClassName: "blorb",
PlanName: "plumbus",
OSBGUID: "ea6d2fc8-0bb8-11e7-af5d-0242ac110005",
}

unversionedChecksum := unversioned.InstanceSpecChecksum(spec)

versionedSpec := v1alpha1.InstanceSpec{}
v1alpha1.Convert_servicecatalog_InstanceSpec_To_v1alpha1_InstanceSpec(&spec, &versionedSpec, nil /* conversionScope */)
versionedChecksum := checksumv1alpha1.InstanceSpecChecksum(versionedSpec)

if e, a := unversionedChecksum, versionedChecksum; e != a {
t.Fatalf("versioned and unversioned checksums should match; expected %v, got %v", e, a)
}
}

// TestBindingChecksum tests that an internal and v1alpha1 checksum of the same object are equivalent
func TestBindingSpecChecksum(t *testing.T) {
spec := servicecatalog.BindingSpec{
InstanceRef: v1.LocalObjectReference{Name: "test-instance"},
SecretName: "test-secret",
OSBGUID: "1995a7e6-d078-4ce6-9057-bcefd793634e",
}

unversionedChecksum := unversioned.BindingSpecChecksum(spec)

versionedSpec := v1alpha1.BindingSpec{}
v1alpha1.Convert_servicecatalog_BindingSpec_To_v1alpha1_BindingSpec(&spec, &versionedSpec, nil /* conversionScope */)
versionedChecksum := checksumv1alpha1.BindingSpecChecksum(versionedSpec)

if e, a := unversionedChecksum, versionedChecksum; e != a {
t.Fatalf("versioned and unversioned checksums should match; expected %v, got %v", e, a)
}
}
66 changes: 66 additions & 0 deletions pkg/apis/servicecatalog/checksum/unversioned/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2017 The Kubernetes 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.
*/

package unversioned

import (
"crypto/sha256"
"fmt"

"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog"
)

// InstanceSpecChecksum calculates a checksum of the given InstanceSpec based
// on the following fields;
//
// - ServiceClassName
// - PlanName
// - Parameters
// - OSBGUID
func InstanceSpecChecksum(spec servicecatalog.InstanceSpec) string {
specString := ""
specString += fmt.Sprintf("serviceClassName: %v\n", spec.ServiceClassName)
specString += fmt.Sprintf("planName: %v\n", spec.PlanName)

if spec.Parameters != nil {
specString += fmt.Sprintf("parameters:\n\n%v\n\n", string(spec.Parameters.Raw))
}

specString += fmt.Sprintf("osbGuid: %v\n", spec.OSBGUID)

sum := sha256.Sum256([]byte(specString))
return fmt.Sprintf("%x", sum)
}

// BindingSpecChecksum calculates a checksum of the given BindingSpec based on
// the following fields:
//
// - InstanceRef.Name
// - Parameters
// - OSBGUID
func BindingSpecChecksum(spec servicecatalog.BindingSpec) string {
specString := ""
specString += fmt.Sprintf("instanceRef: %v\n", spec.InstanceRef.Name)

if spec.Parameters != nil {
specString += fmt.Sprintf("parameters:\n\n%v\n\n", string(spec.Parameters.Raw))
}

specString += fmt.Sprintf("osbGuid: %v\n", spec.OSBGUID)

sum := sha256.Sum256([]byte(specString))
return fmt.Sprintf("%x", sum)
}
66 changes: 66 additions & 0 deletions pkg/apis/servicecatalog/checksum/versioned/v1alpha1/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2017 The Kubernetes 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.
*/

package unversioned

import (
"crypto/sha256"
"fmt"

"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1alpha1"
)

// InstanceSpecChecksum calculates a checksum of the given InstanceSpec based
// on the following fields;
//
// - ServiceClassName
// - PlanName
// - Parameters
// - OSBGUID
func InstanceSpecChecksum(spec v1alpha1.InstanceSpec) string {
specString := ""
specString += fmt.Sprintf("serviceClassName: %v\n", spec.ServiceClassName)
specString += fmt.Sprintf("planName: %v\n", spec.PlanName)

if spec.Parameters != nil {
specString += fmt.Sprintf("parameters:\n\n%v\n\n", string(spec.Parameters.Raw))
}

specString += fmt.Sprintf("osbGuid: %v\n", spec.OSBGUID)

sum := sha256.Sum256([]byte(specString))
return fmt.Sprintf("%x", sum)
}

// BindingSpecChecksum calculates a checksum of the given BindingSpec based on
// the following fields:
//
// - InstanceRef.Name
// - Parameters
// - OSBGUID
func BindingSpecChecksum(spec v1alpha1.BindingSpec) string {
specString := ""
specString += fmt.Sprintf("instanceRef: %v\n", spec.InstanceRef.Name)

if spec.Parameters != nil {
specString += fmt.Sprintf("parameters:\n\n%v\n\n", string(spec.Parameters.Raw))
}

specString += fmt.Sprintf("osbGuid: %v\n", spec.OSBGUID)

sum := sha256.Sum256([]byte(specString))
return fmt.Sprintf("%x", sum)
}
8 changes: 8 additions & 0 deletions pkg/apis/servicecatalog/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ type InstanceSpec struct {
// OSB-specific
OSBDashboardURL *string
OSBLastOperation *string

// Checksum is the checksum of the InstanceSpec that was last successfully
// reconciled against the broker.
Checksum *string
}

// InstanceStatus represents the current status of an Instance.
Expand Down Expand Up @@ -250,6 +254,10 @@ type BindingSpec struct {
// OSBGUID is the identity of this object for use with the OSB API.
// Immutable.
OSBGUID string

// Checksum is the checksum of the BindingSpec that was last successfully
// reconciled against the broker.
Checksum *string
}

// BindingStatus represents the current status of a Binding.
Expand Down
Loading

0 comments on commit c2a9dfb

Please sign in to comment.