mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-09 17:55:10 -04:00
internal: move components into their own package
This commit is contained in:
parent
433e9cdd8b
commit
3637909a46
18 changed files with 156 additions and 135 deletions
90
internal/versions/components/components.go
Normal file
90
internal/versions/components/components.go
Normal file
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package components
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/bootstrapper/initproto"
|
||||
"github.com/edgelesssys/constellation/v2/joinservice/joinproto"
|
||||
)
|
||||
|
||||
// Component is a Kubernetes component.
|
||||
type Component struct {
|
||||
URL string
|
||||
Hash string
|
||||
InstallPath string
|
||||
Extract bool
|
||||
}
|
||||
|
||||
// Components is a list of Kubernetes components.
|
||||
type Components []Component
|
||||
|
||||
// NewComponentsFromInitProto converts a protobuf KubernetesVersion to Components.
|
||||
func NewComponentsFromInitProto(protoComponents []*initproto.KubernetesComponent) Components {
|
||||
components := Components{}
|
||||
for _, protoComponent := range protoComponents {
|
||||
if protoComponent == nil {
|
||||
continue
|
||||
}
|
||||
components = append(components, Component{URL: protoComponent.Url, Hash: protoComponent.Hash, InstallPath: protoComponent.InstallPath, Extract: protoComponent.Extract})
|
||||
}
|
||||
return components
|
||||
}
|
||||
|
||||
// NewComponentsFromJoinProto converts a protobuf KubernetesVersion to Components.
|
||||
func NewComponentsFromJoinProto(protoComponents []*joinproto.KubernetesComponent) Components {
|
||||
components := Components{}
|
||||
for _, protoComponent := range protoComponents {
|
||||
if protoComponent == nil {
|
||||
continue
|
||||
}
|
||||
components = append(components, Component{URL: protoComponent.Url, Hash: protoComponent.Hash, InstallPath: protoComponent.InstallPath, Extract: protoComponent.Extract})
|
||||
}
|
||||
return components
|
||||
}
|
||||
|
||||
// ToInitProto converts Components to a protobuf KubernetesVersion.
|
||||
func (c Components) ToInitProto() []*initproto.KubernetesComponent {
|
||||
protoComponents := []*initproto.KubernetesComponent{}
|
||||
for _, component := range c {
|
||||
protoComponents = append(protoComponents, &initproto.KubernetesComponent{Url: component.URL, Hash: component.Hash, InstallPath: component.InstallPath, Extract: component.Extract})
|
||||
}
|
||||
return protoComponents
|
||||
}
|
||||
|
||||
// ToJoinProto converts Components to a protobuf KubernetesVersion.
|
||||
func (c Components) ToJoinProto() []*joinproto.KubernetesComponent {
|
||||
protoComponents := []*joinproto.KubernetesComponent{}
|
||||
for _, component := range c {
|
||||
protoComponents = append(protoComponents, &joinproto.KubernetesComponent{Url: component.URL, Hash: component.Hash, InstallPath: component.InstallPath, Extract: component.Extract})
|
||||
}
|
||||
return protoComponents
|
||||
}
|
||||
|
||||
// GetHash returns the hash over all component hashes.
|
||||
func (c Components) GetHash() string {
|
||||
sha := sha256.New()
|
||||
for _, component := range c {
|
||||
sha.Write([]byte(component.Hash))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("sha256:%x", sha.Sum(nil))
|
||||
}
|
||||
|
||||
// GetKubeadmComponent returns the kubeadm component.
|
||||
func (c Components) GetKubeadmComponent() (Component, error) {
|
||||
for _, component := range c {
|
||||
if strings.Contains(component.URL, "kubeadm") {
|
||||
return component, nil
|
||||
}
|
||||
}
|
||||
return Component{}, errors.New("kubeadm component not found")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue