mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-04-15 07:16:34 +00:00
Preflight server and CLI
This commit is contained in:
@@ -34,11 +34,6 @@ import (
|
||||
|
||||
var log = logf.Log.WithName("controller")
|
||||
|
||||
/**
|
||||
* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
|
||||
* business logic. Delete these comments after modifying this file.*
|
||||
*/
|
||||
|
||||
// Add creates a new Collector Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
|
||||
// and Start it when the Manager is Started.
|
||||
func Add(mgr manager.Manager) error {
|
||||
|
||||
@@ -18,18 +18,12 @@ package preflight
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
"sigs.k8s.io/controller-runtime/pkg/handler"
|
||||
"sigs.k8s.io/controller-runtime/pkg/manager"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
@@ -39,11 +33,6 @@ import (
|
||||
|
||||
var log = logf.Log.WithName("controller")
|
||||
|
||||
/**
|
||||
* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
|
||||
* business logic. Delete these comments after modifying this file.*
|
||||
*/
|
||||
|
||||
// Add creates a new Preflight Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
|
||||
// and Start it when the Manager is Started.
|
||||
func Add(mgr manager.Manager) error {
|
||||
@@ -69,16 +58,6 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO(user): Modify this to be the types you create
|
||||
// Uncomment watch a Deployment created by Preflight - change this for objects you create
|
||||
err = c.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForOwner{
|
||||
IsController: true,
|
||||
OwnerType: &troubleshootv1beta1.Preflight{},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -104,7 +83,7 @@ func (r *ReconcilePreflight) Reconcile(request reconcile.Request) (reconcile.Res
|
||||
instance := &troubleshootv1beta1.Preflight{}
|
||||
err := r.Get(context.TODO(), request.NamespacedName, instance)
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
if kuberneteserrors.IsNotFound(err) {
|
||||
// Object not found, return. Created objects are automatically garbage collected.
|
||||
// For additional cleanup logic use finalizers.
|
||||
return reconcile.Result{}, nil
|
||||
@@ -113,55 +92,5 @@ func (r *ReconcilePreflight) Reconcile(request reconcile.Request) (reconcile.Res
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
// TODO(user): Change this to be the object type created by your controller
|
||||
// Define the desired Deployment object
|
||||
deploy := &appsv1.Deployment{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: instance.Name + "-deployment",
|
||||
Namespace: instance.Namespace,
|
||||
},
|
||||
Spec: appsv1.DeploymentSpec{
|
||||
Selector: &metav1.LabelSelector{
|
||||
MatchLabels: map[string]string{"deployment": instance.Name + "-deployment"},
|
||||
},
|
||||
Template: corev1.PodTemplateSpec{
|
||||
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"deployment": instance.Name + "-deployment"}},
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
Name: "nginx",
|
||||
Image: "nginx",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := controllerutil.SetControllerReference(instance, deploy, r.scheme); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
// TODO(user): Change this for the object type created by your controller
|
||||
// Check if the Deployment already exists
|
||||
found := &appsv1.Deployment{}
|
||||
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
|
||||
if err != nil && errors.IsNotFound(err) {
|
||||
log.Info("Creating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
|
||||
err = r.Create(context.TODO(), deploy)
|
||||
return reconcile.Result{}, err
|
||||
} else if err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
// TODO(user): Change this for the object type created by your controller
|
||||
// Update the found object and write the result back if there are any changes
|
||||
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
|
||||
found.Spec = deploy.Spec
|
||||
log.Info("Updating Deployment", "namespace", deploy.Namespace, "name", deploy.Name)
|
||||
err = r.Update(context.TODO(), found)
|
||||
if err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
}
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package preflightjob
|
||||
|
||||
func (r *ReconcilePreflightJob) reconcileAnalyzers() error {
|
||||
func (r *ReconcilePreflightJob) reconcilePreflightAnalyzers() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
package preflightjob
|
||||
|
||||
func (r *ReconcilePreflightJob) reconcileCollectors() error {
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
|
||||
// troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1"
|
||||
// kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
// "sigs.k8s.io/controller-runtime/pkg/client/config"
|
||||
)
|
||||
|
||||
func (r *ReconcilePreflightJob) reconcilePreflightCollectors(instance *troubleshootv1beta1.PreflightJob, preflight *troubleshootv1beta1.Preflight) error {
|
||||
for _, collector := range preflight.Spec.Collectors {
|
||||
fmt.Printf("collector = %#v\n", collector)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,119 +1,126 @@
|
||||
package preflightjob
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
)
|
||||
|
||||
func (r *ReconcilePreflightJob) createPreflightServer(instance *troubleshootv1beta1.PreflightJob) error {
|
||||
name := fmt.Sprintf("%s-%s", instance.Name, "preflight")
|
||||
fmt.Printf("name = %s\n", name)
|
||||
// namespacedName := types.NamespacedName{
|
||||
// Name: name,
|
||||
// Namespace: instance.Namespace,
|
||||
// }
|
||||
namespacedName := types.NamespacedName{
|
||||
Name: name,
|
||||
Namespace: instance.Namespace,
|
||||
}
|
||||
|
||||
// found := &corev1.Pod{}
|
||||
// err := r.Get(context.Background(), namespacedName, found)
|
||||
// if err == nil || !kuberneteserrors.IsNotFound(err) {
|
||||
// return err
|
||||
// }
|
||||
found := &corev1.Pod{}
|
||||
err := r.Get(context.Background(), namespacedName, found)
|
||||
if err == nil || !kuberneteserrors.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
// imageName := "replicatedhq/troubleshoot:latest"
|
||||
// imagePullPolicy := corev1.PullAlways
|
||||
imageName := "replicatedhq/troubleshoot:latest"
|
||||
imagePullPolicy := corev1.PullAlways
|
||||
|
||||
// if instance.Spec.Image != "" {
|
||||
// imageName = instance.Spec.Image
|
||||
// }
|
||||
// if instance.Spec.ImagePullPolicy != "" {
|
||||
// imagePullPolicy = corev1.PullPolicy(instance.Spec.ImagePullPolicy)
|
||||
// }
|
||||
if instance.Spec.Image != "" {
|
||||
imageName = instance.Spec.Image
|
||||
}
|
||||
if instance.Spec.ImagePullPolicy != "" {
|
||||
imagePullPolicy = corev1.PullPolicy(instance.Spec.ImagePullPolicy)
|
||||
}
|
||||
|
||||
// podLabels := make(map[string]string)
|
||||
// podLabels["collector"] = instance.Name
|
||||
// podLabels["troubleshoot-role"] = "collector"
|
||||
podLabels := make(map[string]string)
|
||||
podLabels["preflight"] = instance.Name
|
||||
podLabels["troubleshoot-role"] = "preflight"
|
||||
|
||||
// pod := corev1.Pod{
|
||||
// ObjectMeta: metav1.ObjectMeta{
|
||||
// Name: name,
|
||||
// Namespace: instance.Namespace,
|
||||
// Labels: podLabels,
|
||||
// },
|
||||
// TypeMeta: metav1.TypeMeta{
|
||||
// APIVersion: "v1",
|
||||
// Kind: "Pod",
|
||||
// },
|
||||
// Spec: corev1.PodSpec{
|
||||
// RestartPolicy: corev1.RestartPolicyNever,
|
||||
// Containers: []corev1.Container{
|
||||
// {
|
||||
// Image: imageName,
|
||||
// ImagePullPolicy: imagePullPolicy,
|
||||
// Name: "collector",
|
||||
// Command: []string{"collector"},
|
||||
// Args: []string{"server"},
|
||||
// Ports: []corev1.ContainerPort{
|
||||
// {
|
||||
// Name: "http",
|
||||
// ContainerPort: 8000,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// }
|
||||
pod := corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: instance.Namespace,
|
||||
Labels: podLabels,
|
||||
},
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "Pod",
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
RestartPolicy: corev1.RestartPolicyNever,
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
Image: imageName,
|
||||
ImagePullPolicy: imagePullPolicy,
|
||||
Name: "preflight",
|
||||
Command: []string{"preflight"},
|
||||
Args: []string{"server"},
|
||||
Ports: []corev1.ContainerPort{
|
||||
{
|
||||
Name: "http",
|
||||
ContainerPort: 8000,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// if err := controllerutil.SetControllerReference(instance, &pod, r.scheme); err != nil {
|
||||
// return err
|
||||
// }
|
||||
if err := controllerutil.SetControllerReference(instance, &pod, r.scheme); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if err := r.Create(context.Background(), &pod); err != nil {
|
||||
// return err
|
||||
// }
|
||||
if err := r.Create(context.Background(), &pod); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// service := corev1.Service{
|
||||
// ObjectMeta: metav1.ObjectMeta{
|
||||
// Name: name,
|
||||
// Namespace: instance.Namespace,
|
||||
// },
|
||||
// TypeMeta: metav1.TypeMeta{
|
||||
// APIVersion: "v1",
|
||||
// Kind: "Service",
|
||||
// },
|
||||
// Spec: corev1.ServiceSpec{
|
||||
// Selector: podLabels,
|
||||
// Type: corev1.ServiceTypeClusterIP,
|
||||
// Ports: []corev1.ServicePort{
|
||||
// {
|
||||
// Name: "http",
|
||||
// Port: 8000,
|
||||
// TargetPort: intstr.FromInt(8000),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// }
|
||||
service := corev1.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: instance.Namespace,
|
||||
},
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "v1",
|
||||
Kind: "Service",
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Selector: podLabels,
|
||||
Type: corev1.ServiceTypeClusterIP,
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Name: "http",
|
||||
Port: 8000,
|
||||
TargetPort: intstr.FromInt(8000),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// if err := controllerutil.SetControllerReference(instance, &service, r.scheme); err != nil {
|
||||
// return err
|
||||
// }
|
||||
if err := controllerutil.SetControllerReference(instance, &service, r.scheme); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if err := r.Create(context.Background(), &service); err != nil {
|
||||
// return err
|
||||
// }
|
||||
if err := r.Create(context.Background(), &service); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// instance.Status.ServerPodName = name
|
||||
// instance.Status.ServerPodNamespace = instance.Namespace
|
||||
// instance.Status.ServerPodPort = 8000
|
||||
// instance.Status.IsServerReady = true
|
||||
instance.Status.ServerPodName = name
|
||||
instance.Status.ServerPodNamespace = instance.Namespace
|
||||
instance.Status.ServerPodPort = 8000
|
||||
instance.Status.IsServerReady = true
|
||||
|
||||
// // wait for the server to be ready
|
||||
// // TODO
|
||||
// time.Sleep(time.Second * 5)
|
||||
// wait for the server to be ready
|
||||
// TODO
|
||||
time.Sleep(time.Second * 5)
|
||||
|
||||
// if err := r.Update(context.Background(), instance); err != nil {
|
||||
// return err
|
||||
// }
|
||||
if err := r.Update(context.Background(), instance); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package preflightjob
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
|
||||
troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1"
|
||||
@@ -112,7 +111,18 @@ func (r *ReconcilePreflightJob) Reconcile(request reconcile.Request) (reconcile.
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
fmt.Printf("preflightSpec = %#v\n", preflightSpec)
|
||||
// the preflight job will be in collector mode or analyzer mode
|
||||
if !instance.Status.IsCollectorsComplete {
|
||||
if err := r.reconcilePreflightCollectors(instance, preflightSpec); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
} else if !instance.Status.IsAnalyzersComplete {
|
||||
if err := r.reconcilePreflightAnalyzers(); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// just finished, nothing to do
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user