diff --git a/pkg/apis/serving/register.go b/pkg/apis/serving/register.go index da8f024ecb42..9373f9553a98 100644 --- a/pkg/apis/serving/register.go +++ b/pkg/apis/serving/register.go @@ -56,4 +56,8 @@ const ( // ConfigurationGenerationLabelKey is the label key attached to a Revision indicating the // generation of the Configuration that created this revision ConfigurationGenerationLabelKey = GroupName + "/configurationGeneration" + + // BuildHashLabelKey is the label key attached to a Build indicating the + // hash of the spec from which they were created. + BuildHashLabelKey = GroupName + "/buildHash" ) diff --git a/pkg/reconciler/v1alpha1/configuration/resources/build.go b/pkg/reconciler/v1alpha1/configuration/resources/build.go index 6f8e101e2942..5e566edeea61 100644 --- a/pkg/reconciler/v1alpha1/configuration/resources/build.go +++ b/pkg/reconciler/v1alpha1/configuration/resources/build.go @@ -17,6 +17,8 @@ limitations under the License. package resources import ( + "crypto/sha256" + "encoding/hex" "encoding/json" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -24,6 +26,7 @@ import ( buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" "github.com/knative/pkg/kmeta" + "github.com/knative/serving/pkg/apis/serving" "github.com/knative/serving/pkg/apis/serving/v1alpha1" "github.com/knative/serving/pkg/reconciler/v1alpha1/configuration/resources/names" ) @@ -48,6 +51,19 @@ func MakeBuild(config *v1alpha1.Configuration) *unstructured.Unstructured { u = MustToUnstructured(b) } + // After calling `As()` we can be sure that `.Raw` is populated. + + // Compute the hash of the current build's spec. + sum := sha256.Sum256(config.Spec.Build.Raw) + h := hex.EncodeToString(sum[:]) + + // Put it into a label for later lookups. + l := u.GetLabels() + if l == nil { + l = make(map[string]string) + } + l[serving.BuildHashLabelKey] = h[:63] // Labels can only be 63 characters. + u.SetLabels(l) u.SetNamespace(config.Namespace) u.SetName(names.Build(config)) diff --git a/pkg/reconciler/v1alpha1/configuration/resources/build_test.go b/pkg/reconciler/v1alpha1/configuration/resources/build_test.go index 3bd42dfb0714..1b51815c48c3 100644 --- a/pkg/reconciler/v1alpha1/configuration/resources/build_test.go +++ b/pkg/reconciler/v1alpha1/configuration/resources/build_test.go @@ -89,6 +89,9 @@ func TestBuilds(t *testing.T) { "controller": true, "blockOwnerDeletion": true, }}, + "labels": map[string]interface{}{ + "serving.knative.dev/buildHash": "2ee4528bee48a78637ec374eb58cb1977b9611b85545f8b91884ff80b8d9472", + }, "creationTimestamp": nil, }, "spec": map[string]interface{}{ @@ -146,6 +149,9 @@ func TestBuilds(t *testing.T) { "controller": true, "blockOwnerDeletion": true, }}, + "labels": map[string]interface{}{ + "serving.knative.dev/buildHash": "934e535117334c700c3a132e5e9dfc4276974cf2c9d6fe8f09c961f8e058933", + }, "creationTimestamp": nil, }, "spec": map[string]interface{}{