Skip to content

Commit

Permalink
configure sdk chart when included in release
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig O'Donnell committed Sep 19, 2023
1 parent 9f8e40a commit a44e245
Show file tree
Hide file tree
Showing 12 changed files with 1,028 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ require (
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/replicatedhq/kotskinds v0.0.0-20230724164735-f83482cc9cfe
github.com/replicatedhq/kotskinds v0.0.0-20230918191325-9308f41a877c
github.com/replicatedhq/kurlkinds v1.3.6
github.com/replicatedhq/troubleshoot v0.70.3
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,16 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqn
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
github.com/replicatedhq/kotskinds v0.0.0-20230724164735-f83482cc9cfe h1:3AJInd06UxzqHmgy8+24CPsT2tYSE0zToJZyuX9q+MA=
github.com/replicatedhq/kotskinds v0.0.0-20230724164735-f83482cc9cfe/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/kotskinds v0.0.0-20230918171117-a2399c94e661 h1:67BPVTQE8RoXdUoXehSPjkC09oraT8PNPAaGfcSIuE4=
github.com/replicatedhq/kotskinds v0.0.0-20230918171117-a2399c94e661/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/kotskinds v0.0.0-20230918172602-5e3546cee9f0 h1:T4Zy6TPngavCQ2J1/DbZ6+WitXk32TGmgFobxt8p7Lk=
github.com/replicatedhq/kotskinds v0.0.0-20230918172602-5e3546cee9f0/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/kotskinds v0.0.0-20230918190301-7f2ffb4c8f83 h1:QB1SPjMifQMZKaJGeHLYIP1bdZt5Vjqu5JbNTjzvj6U=
github.com/replicatedhq/kotskinds v0.0.0-20230918190301-7f2ffb4c8f83/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/kotskinds v0.0.0-20230918191109-bb200613d590 h1:bc6NOz6f4otUeuwzjSK3AHx8TP6avthDlR5rTozYBCk=
github.com/replicatedhq/kotskinds v0.0.0-20230918191109-bb200613d590/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/kotskinds v0.0.0-20230918191325-9308f41a877c h1:PqJkwiwC2FTjtN9rGLOoS3R1vF2F6n6SgUcEZRyBxrM=
github.com/replicatedhq/kotskinds v0.0.0-20230918191325-9308f41a877c/go.mod h1:QjhIUu3+OmHZ09u09j3FCoTt8F3BYtQglS+OLmftu9I=
github.com/replicatedhq/kurlkinds v1.3.6 h1:/dhS32cSSZR4yS4vA8EquBvz+VgJCyTqBO9Xw+6eI4M=
github.com/replicatedhq/kurlkinds v1.3.6/go.mod h1:c5+hoAkuARgftB2Ft3RCyWRZZPhL0clHEaw7XoGDAg4=
github.com/replicatedhq/termui/v3 v3.1.1-0.20200811145416-f40076d26851 h1:eRlNDHxGfVkPCRXbA4BfQJvt5DHjFiTtWy3R/t4djyY=
Expand Down
30 changes: 17 additions & 13 deletions pkg/k8sutil/kotsadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
types "github.com/replicatedhq/kots/pkg/k8sutil/types"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
"github.com/replicatedhq/kots/pkg/util"
"github.com/segmentio/ksuid"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -81,11 +82,22 @@ func IsKotsadmClusterScoped(ctx context.Context, clientset kubernetes.Interface,
return false
}

func GetKotsadmIDConfigMap() (*corev1.ConfigMap, error) {
clientset, err := GetClientset()
func GetKotsadmClusterID(clientset kubernetes.Interface) string {
var clusterID string
configMap, err := GetKotsadmIDConfigMap(clientset)
if err != nil {
return nil, errors.Wrap(err, "failed to get clientset")
clusterID = ksuid.New().String()
} else if configMap != nil {
clusterID = configMap.Data["id"]
} else {
// configmap is missing for some reason, recreate with new guid, this will appear as a new instance in the report
clusterID = ksuid.New().String()
CreateKotsadmIDConfigMap(clientset, clusterID)
}
return clusterID
}

func GetKotsadmIDConfigMap(clientset kubernetes.Interface) (*corev1.ConfigMap, error) {
namespace := util.PodNamespace
existingConfigmap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), KotsadmIDConfigMapName, metav1.GetOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
Expand All @@ -96,12 +108,8 @@ func GetKotsadmIDConfigMap() (*corev1.ConfigMap, error) {
return existingConfigmap, nil
}

func CreateKotsadmIDConfigMap(kotsadmID string) error {
func CreateKotsadmIDConfigMap(clientset kubernetes.Interface, kotsadmID string) error {
var err error = nil
clientset, err := GetClientset()
if err != nil {
return err
}
configmap := corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Expand Down Expand Up @@ -136,11 +144,7 @@ func IsKotsadmIDConfigMapPresent() (bool, error) {
return true, nil
}

func UpdateKotsadmIDConfigMap(kotsadmID string) error {
clientset, err := GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get clientset")
}
func UpdateKotsadmIDConfigMap(clientset kubernetes.Interface, kotsadmID string) error {
namespace := util.PodNamespace
existingConfigMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), KotsadmIDConfigMapName, metav1.GetOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
Expand Down
159 changes: 159 additions & 0 deletions pkg/kotsutil/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package kotsutil

import (
"bytes"
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/util"
yaml "github.com/replicatedhq/yaml/v3"
goyaml "gopkg.in/yaml.v3"
k8syaml "sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -85,3 +88,159 @@ func removeNilFieldsFromMap(input map[string]interface{}) bool {

return removedItems
}

func MergeYAMLNodes(targetNodes []*goyaml.Node, overrideNodes []*goyaml.Node) []*goyaml.Node {
// Since inputs are arrays and not maps, we need to:
// 1. Copy all keys in targetNodes, overriding the ones that match from overrideNodes
// 2. Add all keys from overrideNodes that don't exist in targetNodes

if len(overrideNodes) == 0 {
return targetNodes
}

if len(targetNodes) == 0 {
return overrideNodes
}

// Special case where top level node is either a mapping node or an array
if len(targetNodes) == 1 && len(overrideNodes) == 1 {
if targetNodes[0].Kind == goyaml.MappingNode && overrideNodes[0].Kind == goyaml.MappingNode {
return []*goyaml.Node{
{
Kind: goyaml.MappingNode,
Content: MergeYAMLNodes(targetNodes[0].Content, overrideNodes[0].Content),
},
}
}

if targetNodes[0].Value == overrideNodes[0].Value {
return overrideNodes
}

return append(targetNodes, overrideNodes...)
}

// 1. Copy all keys in targetNodes, overriding the ones that match from overrideNodes
newNodes := make([]*goyaml.Node, 0)
for i := 0; i < len(targetNodes)-1; i += 2 {
var additionalNode *goyaml.Node
for j := 0; j < len(overrideNodes)-1; j += 2 {
nodeNameI := targetNodes[i]
nodeValueI := targetNodes[i+1]

nodeNameJ := overrideNodes[j]
nodeValueJ := overrideNodes[j+1]

if nodeNameI.Value != nodeNameJ.Value {
continue
}

additionalNode = &goyaml.Node{
Kind: nodeValueJ.Kind,
Tag: nodeValueJ.Tag,
Line: nodeValueJ.Line,
Style: nodeValueJ.Style,
Anchor: nodeValueJ.Anchor,
Value: nodeValueJ.Value,
Alias: nodeValueJ.Alias,
HeadComment: nodeValueJ.HeadComment,
LineComment: nodeValueJ.LineComment,
FootComment: nodeValueJ.FootComment,
Column: nodeValueJ.Column,
}

if nodeValueI.Kind == goyaml.MappingNode && nodeValueJ.Kind == goyaml.MappingNode {
additionalNode.Content = MergeYAMLNodes(nodeValueI.Content, nodeValueJ.Content)
} else {
additionalNode.Content = nodeValueJ.Content
}

break
}

if additionalNode != nil {
newNodes = append(newNodes, targetNodes[i], additionalNode)
} else {
newNodes = append(newNodes, targetNodes[i], targetNodes[i+1])
}
}

// 2. Add all keys from overrideNodes that don't exist in targetNodes
for j := 0; j < len(overrideNodes)-1; j += 2 {
isFound := false
for i := 0; i < len(newNodes)-1; i += 2 {
nodeNameI := newNodes[i]
nodeValueI := newNodes[i+1]

additionalNodeName := overrideNodes[j]
additionalNodeValue := overrideNodes[j+1]

if nodeNameI.Value != additionalNodeName.Value {
continue
}

if nodeValueI.Kind == goyaml.MappingNode && additionalNodeValue.Kind == goyaml.MappingNode {
nodeValueI.Content = MergeYAMLNodes(nodeValueI.Content, additionalNodeValue.Content)
}

isFound = true
break
}

if !isFound {
newNodes = append(newNodes, overrideNodes[j], overrideNodes[j+1])
}
}

return newNodes
}

func ContentToDocNode(doc *goyaml.Node, nodes []*goyaml.Node) *goyaml.Node {
if doc == nil {
return &goyaml.Node{
Kind: goyaml.DocumentNode,
Content: nodes,
}
}
return &goyaml.Node{
Kind: doc.Kind,
Tag: doc.Tag,
Line: doc.Line,
Style: doc.Style,
Anchor: doc.Anchor,
Value: doc.Value,
Alias: doc.Alias,
HeadComment: doc.HeadComment,
LineComment: doc.LineComment,
FootComment: doc.FootComment,
Column: doc.Column,
Content: nodes,
}
}

func NodeToYAML(node *goyaml.Node) ([]byte, error) {
var renderedContents bytes.Buffer
yamlEncoder := goyaml.NewEncoder(&renderedContents)
yamlEncoder.SetIndent(2) // this may change indentations of the original values.yaml, but this matches out tests
err := yamlEncoder.Encode(node)
if err != nil {
return nil, errors.Wrap(err, "marshal")
}

return renderedContents.Bytes(), nil
}

// Handy functions for printing YAML nodes
func PrintNodes(nodes []*goyaml.Node, i int) {
for _, n := range nodes {
PrintNode(n, i)
}
}
func PrintNode(n *goyaml.Node, i int) {
if n == nil {
return
}
indent := strings.Repeat(" ", i*2)
fmt.Printf("%stag:%v, style:%v, kind:%v, value:%v\n", indent, n.Tag, n.Style, n.Kind, n.Value)
PrintNodes(n.Content, i+1)
}
1 change: 1 addition & 0 deletions pkg/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func Pull(upstreamURI string, pullOptions PullOptions) (string, error) {

if installation != nil {
fetchOptions.EncryptionKey = installation.Spec.EncryptionKey
fetchOptions.CurrentReleaseSequence = installation.Spec.ReleaseSequence
fetchOptions.CurrentVersionLabel = installation.Spec.VersionLabel
fetchOptions.CurrentChannelID = installation.Spec.ChannelID
fetchOptions.CurrentChannelName = installation.Spec.ChannelName
Expand Down
22 changes: 9 additions & 13 deletions pkg/reporting/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,18 @@ func initFromDownstream() error {
return errors.Wrap(err, "failed to check configmap")
}

clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get clientset")
}

if isKotsadmIDGenerated && !cmpExists {
kotsadmID := ksuid.New().String()
err = k8sutil.CreateKotsadmIDConfigMap(kotsadmID)
err = k8sutil.CreateKotsadmIDConfigMap(clientset, kotsadmID)
} else if !isKotsadmIDGenerated && !cmpExists {
err = k8sutil.CreateKotsadmIDConfigMap(clusterID)
err = k8sutil.CreateKotsadmIDConfigMap(clientset, clusterID)
} else if !isKotsadmIDGenerated && cmpExists {
err = k8sutil.UpdateKotsadmIDConfigMap(clusterID)
err = k8sutil.UpdateKotsadmIDConfigMap(clientset, clusterID)
} else {
// id exists and so as configmap, noop
}
Expand Down Expand Up @@ -181,16 +186,7 @@ func GetReportingInfo(appID string) *types.ReportingInfo {
if util.IsHelmManaged() {
r.ClusterID = clusterID
} else {
configMap, err := k8sutil.GetKotsadmIDConfigMap()
if err != nil {
r.ClusterID = ksuid.New().String()
} else if configMap != nil {
r.ClusterID = configMap.Data["id"]
} else {
// configmap is missing for some reason, recreate with new guid, this will appear as a new instance in the report
r.ClusterID = ksuid.New().String()
k8sutil.CreateKotsadmIDConfigMap(r.ClusterID)
}
r.ClusterID = k8sutil.GetKotsadmClusterID(clientset)

di, err := getDownstreamInfo(appID)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/rewrite/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func Rewrite(rewriteOptions RewriteOptions) error {
RootDir: rewriteOptions.RootDir,
LocalPath: rewriteOptions.UpstreamPath,
CurrentCursor: rewriteOptions.Installation.Spec.UpdateCursor,
CurrentReleaseSequence: rewriteOptions.Installation.Spec.ReleaseSequence,
CurrentVersionLabel: rewriteOptions.Installation.Spec.VersionLabel,
CurrentVersionIsRequired: rewriteOptions.Installation.Spec.IsRequired,
CurrentReplicatedRegistryDomain: rewriteOptions.Installation.Spec.ReplicatedRegistryDomain,
Expand Down
8 changes: 8 additions & 0 deletions pkg/upstream/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func downloadUpstream(upstreamURI string, fetchOptions *types.FetchOptions) (*ty
fetchOptions.ConfigValues,
fetchOptions.IdentityConfig,
pickCursor(fetchOptions),
pickReleaseSequence(fetchOptions),
pickVersionLabel(fetchOptions),
pickVersionIsRequired(fetchOptions),
pickReplicatedRegistryDomain(fetchOptions),
Expand Down Expand Up @@ -83,6 +84,13 @@ func pickVersionIsRequired(fetchOptions *types.FetchOptions) bool {
return fetchOptions.CurrentVersionIsRequired
}

func pickReleaseSequence(fetchOptions *types.FetchOptions) int64 {
if fetchOptions.Airgap != nil {
return fetchOptions.Airgap.Spec.ReleaseSequence
}
return fetchOptions.CurrentReleaseSequence
}

func pickVersionLabel(fetchOptions *types.FetchOptions) string {
if fetchOptions.Airgap != nil && fetchOptions.Airgap.Spec.VersionLabel != "" {
return fetchOptions.Airgap.Spec.VersionLabel
Expand Down
Loading

0 comments on commit a44e245

Please sign in to comment.