-
Notifications
You must be signed in to change notification settings - Fork 0
/
packagevariant_interfaces.go
76 lines (62 loc) · 2.18 KB
/
packagevariant_interfaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright 2024 Nokia.
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 v1alpha1
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/kform-dev/pkg-server/apis/condition"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetCondition returns the condition based on the condition kind
func (r *PackageVariant) GetCondition(t condition.ConditionType) condition.Condition {
return r.Status.GetCondition(t)
}
// SetConditions sets the conditions on the resource. it allows for 0, 1 or more conditions
// to be set at once
func (r *PackageVariant) SetConditions(c ...condition.Condition) {
r.Status.SetConditions(c...)
}
func GetPackageRevisionName(repo, pkg, ws string) string {
return fmt.Sprintf("%s.%s.%s", repo, PackageToName(pkg), ws)
}
func PackageToName(pkg string) string {
return strings.ReplaceAll(pkg, "/", ":")
}
func (r *PackageVariant) GetWorkspaceName(hash [sha1.Size]byte) string {
//return fmt.Sprintf("pv-%s-%s", r.Name, hex.EncodeToString(hash[:8]))
return fmt.Sprintf("pv-%s", hex.EncodeToString(hash[:8]))
}
func (r PackageVariantSpec) CalculateHash() ([sha1.Size]byte, error) {
// Convert the struct to JSON
jsonData, err := json.Marshal(r)
if err != nil {
return [sha1.Size]byte{}, err
}
// Calculate SHA-1 hash
return sha1.Sum(jsonData), nil
}
// BuildPackageVariant returns an PackageRevision from a client Object a Spec/Status
func BuildPackageVariant(meta metav1.ObjectMeta, spec PackageVariantSpec, status PackageVariantStatus) *PackageVariant {
return &PackageVariant{
TypeMeta: metav1.TypeMeta{
APIVersion: SchemeGroupVersion.Identifier(),
Kind: PackageVariantKind,
},
ObjectMeta: meta,
Spec: spec,
Status: status,
}
}