Skip to content

Commit

Permalink
added logic to lookup smee pod when deployed using local tinkerbell c…
Browse files Browse the repository at this point in the history
…hart
  • Loading branch information
ibrokethecloud committed Mar 21, 2024
1 parent ce84a0e commit 297dd93
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 32 deletions.
1 change: 1 addition & 0 deletions pkg/api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ const (
SeederConfig = "seeder-config"
DefaultEndpointPort = 9090
DefaultSeederDeploymentService = "harvester-seeder-endpoint"
DefaultHegelDeploymentEndpointLookup = "smee"
)
4 changes: 2 additions & 2 deletions pkg/controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,13 @@ func (r *ClusterReconciler) createTinkerbellHardware(ctx context.Context, cObj *

// check to see if the service for tink-stack is ready
tinkStackService := &corev1.Service{}
err := r.Get(ctx, types.NamespacedName{Name: seederv1alpha1.DefaultTinkStackService, Namespace: namespace}, tinkStackService)
err := r.Get(ctx, types.NamespacedName{Name: seederv1alpha1.DefaultTinkStackService, Namespace: deploymentNamespace}, tinkStackService)
if err != nil {
return fmt.Errorf("error fetching svc %s in ns %s: %v", seederv1alpha1.DefaultTinkStackService, seederv1alpha1.DefaultLocalClusterNamespace, err)
}

seederDeploymentService := &corev1.Service{}
err = r.Get(ctx, types.NamespacedName{Name: seederv1alpha1.DefaultSeederDeploymentService, Namespace: namespace}, seederDeploymentService)
err = r.Get(ctx, types.NamespacedName{Name: seederv1alpha1.DefaultSeederDeploymentService, Namespace: deploymentNamespace}, seederDeploymentService)

if err != nil {
return fmt.Errorf("error fetching svc %s in ns %s: %v", seederv1alpha1.DefaultSeederDeploymentService, seederv1alpha1.DefaultLocalClusterNamespace, err)
Expand Down
49 changes: 45 additions & 4 deletions pkg/controllers/cluster_tinkerbell_template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
tinkv1alpha1 "github.com/tinkerbell/tink/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -71,10 +72,12 @@ func (r *ClusterTinkerbellTemplateReconciler) createTinkerbellTemplate(ctx conte
c := cObj.DeepCopy()
if c.Status.Status == seederv1alpha1.ClusterNodesPatched || c.Status.Status == seederv1alpha1.ClusterTinkHardwareSubmitted || c.Status.Status == seederv1alpha1.ClusterRunning {
// check to see if the service for tink-stack is ready
tinkStackService := &corev1.Service{}
err := r.Get(ctx, types.NamespacedName{Name: seederv1alpha1.DefaultTinkStackService, Namespace: namespace}, tinkStackService)
// if using an external tinkerbell stack this service should be ready as HegelEndpoint is used for serving harvester config via userdata on hardware object
// if using the custom tinkerbell chart in seeder repo, then tink-stack is not deployed but an nginx server is deployed
// to run with smee/boots, and this serves as a proxy to hegel endpoint, and we should switch to the same
hegelEndpoint, err := r.fetchHegelEndpoint(ctx)
if err != nil {
return fmt.Errorf("error fetching svc %s in ns %s: %v", seederv1alpha1.DefaultTinkStackService, c.Namespace, err)
return fmt.Errorf("fetching hegel endpoint: %v", err)
}

seederConfig := &corev1.ConfigMap{}
Expand All @@ -98,7 +101,7 @@ func (r *ClusterTinkerbellTemplateReconciler) createTinkerbellTemplate(ctx conte
continue
}

template, err := tink.GenerateTemplate(tinkStackService, seederConfig, inventory, c)
template, err := tink.GenerateTemplate(hegelEndpoint, seederConfig, inventory, c)
if err != nil {
return err
}
Expand Down Expand Up @@ -164,3 +167,41 @@ func (r *ClusterTinkerbellTemplateReconciler) SetupWithManager(mgr ctrl.Manager)
})).
Complete(r)
}

func (r *ClusterTinkerbellTemplateReconciler) fetchHegelEndpoint(ctx context.Context) (string, error) {
// check to see if the service for tink-stack is ready
// if using an external tinkerbell stack this service should be ready as HegelEndpoint is used for serving harvester config via userdata on hardware object
// if using the custom tinkerbell chart in seeder repo, then tink-stack is not deployed but an nginx server is deployed
// to run with smee/boots, and this serves as a proxy to hegel endpoint, and we should switch to the same
tinkStackService := &corev1.Service{}
err := r.Get(ctx, types.NamespacedName{Name: seederv1alpha1.DefaultTinkStackService, Namespace: deploymentNamespace}, tinkStackService)

if err != nil {
if !apierrors.IsNotFound(err) {
return "", fmt.Errorf("error fetching svc %s in ns %s: %v", seederv1alpha1.DefaultTinkStackService, deploymentNamespace, err)
}
} else {
return tinkStackService.Status.LoadBalancer.Ingress[0].IP, nil
}

// lookup smee pod address
podList := &corev1.PodList{}
ls := labels.SelectorFromSet(map[string]string{
"app": seederv1alpha1.DefaultHegelDeploymentEndpointLookup,
"stack": "tinkerbell",
})
err = r.List(ctx, podList, &client.ListOptions{
LabelSelector: ls,
Namespace: deploymentNamespace,
})

if err != nil {
return "nil", err
}

if len(podList.Items) == 0 {
return "", fmt.Errorf("no pods matching smee requirements found")
}
return podList.Items[0].Status.HostIP, nil

}
6 changes: 3 additions & 3 deletions pkg/controllers/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
)

var (
scheme = runtime.NewScheme()
namespace string
scheme = runtime.NewScheme()
deploymentNamespace string //contains name of namespace where seeder is deployed
)

const (
Expand Down Expand Up @@ -75,7 +75,7 @@ func (s *Server) Start(ctx context.Context) error {
}

// used by other methods to lookup tink-stack and harvester-seeder-deployment services
namespace = s.LeaderElectionNamespace
deploymentNamespace = s.LeaderElectionNamespace

// create CRDs
err = crd.Create(ctx, mgr.GetConfig())
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

namespace = "harvester-system"
deploymentNamespace = "harvester-system"
err = createHarvesterSystemNamespace(ctx, k8sClient)
Expect(err).NotTo(HaveOccurred())
err = createTinkStackService(ctx, k8sClient)
Expand Down
10 changes: 6 additions & 4 deletions pkg/tink/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const (
RebootHarvesterImageKey = "reboot-harvester-image"
)

func GenerateTemplate(svc *corev1.Service, cm *corev1.ConfigMap, i *seederv1alpha1.Inventory, c *seederv1alpha1.Cluster) (*tinkv1alpha1.Template, error) {
func GenerateTemplate(hegelEndpoint string, cm *corev1.ConfigMap, i *seederv1alpha1.Inventory, c *seederv1alpha1.Cluster) (*tinkv1alpha1.Template, error) {
template := &tinkv1alpha1.Template{
ObjectMeta: metav1.ObjectMeta{
Name: i.Name,
Namespace: i.Namespace,
},
}

data, err := generateDataTemplate(svc, cm, i, c)
data, err := generateDataTemplate(hegelEndpoint, cm, i, c)
if err != nil {
return nil, fmt.Errorf("error generating template data: %v", err)
}
Expand All @@ -36,15 +36,17 @@ func GenerateTemplate(svc *corev1.Service, cm *corev1.ConfigMap, i *seederv1alph
return template, nil
}

func generateDataTemplate(svc *corev1.Service, cm *corev1.ConfigMap, i *seederv1alpha1.Inventory, c *seederv1alpha1.Cluster) (*string, error) {
func generateDataTemplate(hegelEndpoint string, cm *corev1.ConfigMap, i *seederv1alpha1.Inventory, c *seederv1alpha1.Cluster) (*string, error) {
//set StreamHarvester action environment variables
DefaultStreamHarvesterAction.Environment[DestDisk] = i.Spec.PrimaryDisk
DefaultStreamHarvesterAction.Environment[ImageURL] = fmt.Sprintf("%s/%s/harvester-%s-%s.raw.gz", c.Spec.ImageURL, c.Spec.HarvesterVersion, c.Spec.HarvesterVersion, i.Spec.Arch)

//set ConfigureHarvester action environment variables
DefaultConfigureHarvesterAction.Environment[HarvesterDevice] = i.Spec.PrimaryDisk
DefaultConfigureHarvesterAction.Environment[HarvesterCloudInitURL] = fmt.Sprintf("http://%s:%s/2009-04-04/user-data",
svc.Status.LoadBalancer.Ingress[0].IP, HegelDefaultPort)
hegelEndpoint, HegelDefaultPort)

//svc.Status.LoadBalancer.Ingress[0].IP

// override images if specified
if cm != nil {
Expand Down
19 changes: 1 addition & 18 deletions pkg/tink/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
seederv1alpha1 "github.com/harvester/seeder/pkg/api/v1alpha1"
"github.com/rancher/wrangler/pkg/yaml"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -33,23 +32,7 @@ func Test_GenerateTemplate(t *testing.T) {
},
}

tinkStackSvc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "tink-stack",
Namespace: "harvester-system",
},
Status: corev1.ServiceStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
IP: "192.168.1.100",
},
},
},
},
}

template, err := GenerateTemplate(tinkStackSvc, nil, i, c)
template, err := GenerateTemplate("192.168.1.100", nil, i, c)
assert.NoError(err, "exppected no error during template generation")
assert.Equal(template.Name, i.Name, "expected template name to match inventory name")
assert.Equal(template.Namespace, i.Namespace, "expected template namespace to match inventory namespace")
Expand Down

0 comments on commit 297dd93

Please sign in to comment.