Skip to content

Commit

Permalink
Add more verbose error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmasi committed Aug 1, 2023
1 parent f8fbd7d commit b61ddda
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 56 deletions.
87 changes: 44 additions & 43 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,25 @@ func (d *Deployment) Deploy(ctx context.Context, kubecfg string) (rerr error) {
defer func() { finish(rerr) }()
}
if err := d.checkDependencies(); err != nil {
return err
return fmt.Errorf("failed to check for dependencies: %w", err)
}
log.Infof("Deploying cluster...")
if err := d.Cluster.Deploy(ctx); err != nil {
return err
return fmt.Errorf("failed to deploy cluster: %w", err)
}
log.Infof("Cluster deployed")
if err := d.Cluster.Healthy(); err != nil {
return err
return fmt.Errorf("failed to check cluster is healthy: %w", err)
}
log.Infof("Cluster healthy")
// Once cluster is up, set kClient
rCfg, err := clientcmd.BuildConfigFromFlags("", kubecfg)
if err != nil {
return err
return fmt.Errorf("failed to create k8s config: %w", err)
}
kClient, err := kubernetes.NewForConfig(rCfg)
if err != nil {
return err
return fmt.Errorf("failed to create k8s client: %w", err)
}

log.Infof("Checking kubectl versions.")
Expand All @@ -252,11 +252,11 @@ func (d *Deployment) Deploy(ctx context.Context, kubecfg string) (rerr error) {
}
kClientVersion, err := getVersion(kubeYAML.ClientVersion.GitVersion)
if err != nil {
return err
return fmt.Errorf("failed to parse k8s client version: %w", err)
}
kServerVersion, err := getVersion(kubeYAML.ServerVersion.GitVersion)
if err != nil {
return err
return fmt.Errorf("failed to parse k8s server version: %w", err)
}
origMajor := kClientVersion.Major
kClientVersion.Major -= 2
Expand Down Expand Up @@ -298,35 +298,35 @@ func (d *Deployment) Deploy(ctx context.Context, kubecfg string) (rerr error) {

log.Infof("Deploying ingress...")
if err := d.Ingress.Deploy(ctx); err != nil {
return err
return fmt.Errorf("failed to deploy ingress: %w", err)
}
tCtx, cancel := context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := d.Ingress.Healthy(tCtx); err != nil {
return err
return fmt.Errorf("failed to check ingress is healthy: %w", err)
}
log.Infof("Ingress healthy")
log.Infof("Deploying CNI...")
if err := d.CNI.Deploy(ctx); err != nil {
return err
return fmt.Errorf("failed to deploy CNI: %w", err)
}
d.CNI.SetKClient(kClient)
tCtx, cancel = context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := d.CNI.Healthy(tCtx); err != nil {
return err
return fmt.Errorf("failed to check CNI is healthy: %w", err)
}
log.Infof("CNI healthy")
for _, c := range d.Controllers {
log.Infof("Deploying controller...")
if err := c.Deploy(ctx); err != nil {
return err
return fmt.Errorf("failed to deploy controller: %w", err)
}
c.SetKClient(kClient)
tCtx, cancel = context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := c.Healthy(tCtx); err != nil {
return err
return fmt.Errorf("failed to check controller is healthy: %w", err)
}
}
log.Infof("Controllers deployed and healthy")
Expand All @@ -336,34 +336,34 @@ func (d *Deployment) Deploy(ctx context.Context, kubecfg string) (rerr error) {
func (d *Deployment) Delete() error {
log.Infof("Deleting cluster...")
if err := d.Cluster.Delete(); err != nil {
return err
return fmt.Errorf("failed to delete cluster: %w", err)
}
log.Infof("Cluster deleted")
return nil
}

func (d *Deployment) Healthy(ctx context.Context) error {
if err := d.Cluster.Healthy(); err != nil {
return err
return fmt.Errorf("failed to check cluster is healthy: %w", err)
}
log.Infof("Cluster healthy")
tCtx, cancel := context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := d.Ingress.Healthy(tCtx); err != nil {
return err
return fmt.Errorf("failed to check ingress is healthy: %w", err)
}
log.Infof("Ingress healthy")
tCtx, cancel = context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := d.CNI.Healthy(tCtx); err != nil {
return err
return fmt.Errorf("failed to check CNI is healthy: %w", err)
}
log.Infof("CNI healthy")
for _, c := range d.Controllers {
tCtx, cancel = context.WithTimeout(ctx, healthTimeout)
defer cancel()
if err := c.Healthy(tCtx); err != nil {
return err
return fmt.Errorf("failed to check controller is healthy: %w", err)
}
}
log.Infof("Controllers healthy")
Expand Down Expand Up @@ -488,7 +488,7 @@ func (k *KindSpec) checkDependencies() error {
if k.Version != "" {
wantV, err := getVersion(k.Version)
if err != nil {
return err
return fmt.Errorf("failed to parse desired kind version: %w", err)
}

stdout, err := outCommand("kind", "version")
Expand Down Expand Up @@ -545,28 +545,29 @@ func (k *KindSpec) create() error {
args = append(args, "--config", k.KindConfigFile)
}
log.Infof("Creating kind cluster with: %v", args)
// TODO
if err := logCommand("kind", args...); err != nil {
return fmt.Errorf("failed to create cluster: %w", err)
return err
}
log.Infof("Deployed kind cluster: %s", k.Name)
return nil
}

func (k *KindSpec) Deploy(ctx context.Context) error {
if err := k.checkDependencies(); err != nil {
return err
return fmt.Errorf("failed to check for dependencies: %w", err)
}

if err := k.create(); err != nil {
return err
return fmt.Errorf("failed to create kind cluster: %w", err)
}

// If the script is found, then run it. Else silently ignore it.
// The set_pid_max script modifies the kernel.pid_max value to
// be acceptable for the Cisco 8000e container.
if _, err := os.Stat(setPIDMaxScript); err == nil {
if err := logCommand(setPIDMaxScript); err != nil {
return err
return fmt.Errorf("failed to exec set_pid_max script: %w", err)
}
}

Expand Down Expand Up @@ -600,7 +601,7 @@ func (k *KindSpec) Delete() error {
args = append(args, "--name", k.Name)
}
if err := logCommand("kind", args...); err != nil {
return fmt.Errorf("failed to delete cluster using cli: %w", err)
return fmt.Errorf("failed to delete cluster: %w", err)
}
return nil
}
Expand Down Expand Up @@ -820,13 +821,13 @@ func (m *MetalLBSpec) Deploy(ctx context.Context) error {
if m.dClient == nil {
m.dClient, err = dclient.NewClientWithOpts(dclient.FromEnv)
if err != nil {
return err
return fmt.Errorf("failed to create docker client: %w", err)
}
}
if m.mClient == nil {
m.mClient, err = metallbclientv1.NewForConfig(m.rCfg)
if err != nil {
return err
return fmt.Errorf("failed to create metallb client: %w", err)
}
}

Expand All @@ -851,7 +852,7 @@ func (m *MetalLBSpec) Deploy(ctx context.Context) error {
}
log.Infof("Deploying MetalLB from: %s", m.Manifest)
if err := logCommand("kubectl", "apply", "-f", m.Manifest); err != nil {
return err
return fmt.Errorf("failed to deploy metallb: %w", err)
}
if _, err := m.kClient.CoreV1().Secrets("metallb-system").Get(ctx, "memberlist", metav1.GetOptions{}); err != nil {
log.Infof("Creating metallb secret")
Expand All @@ -868,21 +869,21 @@ func (m *MetalLBSpec) Deploy(ctx context.Context) error {
},
}
if _, err := m.kClient.CoreV1().Secrets("metallb-system").Create(ctx, s, metav1.CreateOptions{}); err != nil {
return err
return fmt.Errorf("failed to create metallb secret: %w", err)
}
}

// Wait for metallb to be healthy
if err := m.Healthy(ctx); err != nil {
return err
return fmt.Errorf("metallb not healthy: %w", err)
}

if _, err = m.mClient.IPAddressPool("metallb-system").Get(ctx, "kne-service-pool", metav1.GetOptions{}); err != nil {
log.Infof("Applying metallb ingress config")
// Get Network information from docker.
nr, err := m.dClient.NetworkList(ctx, dtypes.NetworkListOptions{})
if err != nil {
return err
return fmt.Errorf("failed to get docker network list: %w", err)
}
var network dtypes.NetworkResource
for _, v := range nr {
Expand All @@ -899,7 +900,7 @@ func (m *MetalLBSpec) Deploy(ctx context.Context) error {
for _, ipRange := range network.IPAM.Config {
_, ipNet, err := net.ParseCIDR(ipRange.Subnet)
if err != nil {
return err
return fmt.Errorf("failed to parse cidr: %w", err)
}
if ipNet.IP.To4() != nil {
n = ipNet
Expand Down Expand Up @@ -932,7 +933,7 @@ func (m *MetalLBSpec) Deploy(ctx context.Context) error {
},
}
if _, err = m.mClient.L2Advertisement("metallb-system").Create(ctx, l2Advert, metav1.CreateOptions{}); err != nil {
return err
return fmt.Errorf("failed to create metallb L2 advertisement: %w", err)
}
}
return nil
Expand Down Expand Up @@ -981,7 +982,7 @@ func (m *MeshnetSpec) Deploy(ctx context.Context) error {
}
log.Infof("Deploying Meshnet from: %s", m.Manifest)
if err := logCommand("kubectl", "apply", "-f", m.Manifest); err != nil {
return err
return fmt.Errorf("failed to deploy meshnet: %w", err)
}
log.Infof("Meshnet Deployed")
return nil
Expand All @@ -998,10 +999,10 @@ func (m *MeshnetSpec) Healthy(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return fmt.Errorf("context canceled before healthy")
return fmt.Errorf("context canceled before meshnet healthy")
case e, ok := <-w.ResultChan():
if !ok {
return fmt.Errorf("watch channel closed before healthy")
return fmt.Errorf("watch channel closed before meshnet healthy")
}
d, ok := e.Object.(*appsv1.DaemonSet)
if !ok {
Expand Down Expand Up @@ -1055,7 +1056,7 @@ func (c *CEOSLabSpec) Deploy(ctx context.Context) error {
}
log.Infof("Deploying CEOSLab controller from: %s", c.Operator)
if err := logCommand("kubectl", "apply", "-f", c.Operator); err != nil {
return err
return fmt.Errorf("failed to deploy ceoslab operator: %w", err)
}
log.Infof("CEOSLab controller deployed")
return nil
Expand Down Expand Up @@ -1104,7 +1105,7 @@ func (l *LemmingSpec) Deploy(ctx context.Context) error {
}
log.Infof("Deploying Lemming controller from: %s", l.Operator)
if err := logCommand("kubectl", "apply", "-f", l.Operator); err != nil {
return err
return fmt.Errorf("failed to deploy lemming operator: %w", err)
}
log.Infof("Lemming controller deployed")
return nil
Expand Down Expand Up @@ -1153,7 +1154,7 @@ func (s *SRLinuxSpec) Deploy(ctx context.Context) error {
}
log.Infof("Deploying SRLinux controller from: %s", s.Operator)
if err := logCommand("kubectl", "apply", "-f", s.Operator); err != nil {
return err
return fmt.Errorf("failed to deploy srlinux operator: %w", err)
}
log.Infof("SRLinux controller deployed")
return nil
Expand Down Expand Up @@ -1204,7 +1205,7 @@ func (i *IxiaTGSpec) Deploy(ctx context.Context) error {
}
log.Infof("Deploying IxiaTG controller from: %s", i.Operator)
if err := logCommand("kubectl", "apply", "-f", i.Operator); err != nil {
return err
return fmt.Errorf("failed to deploy ixiatg operator: %w", err)
}

if i.ConfigMap == "" && i.ManifestDir != "" {
Expand Down Expand Up @@ -1238,7 +1239,7 @@ func (i *IxiaTGSpec) Deploy(ctx context.Context) error {
}
log.Infof("Deploying IxiaTG config map from: %s", i.ConfigMap)
if err := logCommand("kubectl", "apply", "-f", i.ConfigMap); err != nil {
return err
return fmt.Errorf("failed to deploy ixiatg config map: %w", err)
}
log.Infof("IxiaTG controller deployed")
return nil
Expand All @@ -1252,16 +1253,16 @@ func deploymentHealthy(ctx context.Context, c kubernetes.Interface, name string)
log.Infof("Waiting on deployment %q to be healthy", name)
w, err := c.AppsV1().Deployments(name).Watch(ctx, metav1.ListOptions{})
if err != nil {
return err
return fmt.Errorf("failed to create watcher for deployment %q", name)
}
ch := w.ResultChan()
for {
select {
case <-ctx.Done():
return fmt.Errorf("context canceled before healthy")
return fmt.Errorf("context canceled before %q healthy", name)
case e, ok := <-ch:
if !ok {
return fmt.Errorf("watch channel closed before healthy")
return fmt.Errorf("watch channel closed before %q healthy", name)
}
d, ok := e.Object.(*appsv1.Deployment)
if !ok {
Expand Down
5 changes: 5 additions & 0 deletions topo/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Interface interface {
Name() string
GetNamespace() string
GetProto() *tpb.Node
String() string
}

type Implementation interface {
Expand Down Expand Up @@ -140,6 +141,10 @@ func (n *Impl) GetNamespace() string {
return n.Namespace
}

func (n *Impl) String() string {
return fmt.Sprintf("%q (vendor: %q, model: %q)", n.Proto.Name, n.Proto.Vendor, n.Proto.Model)
}

func (n *Impl) TopologySpecs(context.Context) ([]*topologyv1.Topology, error) {
proto := n.GetProto()

Expand Down
Loading

0 comments on commit b61ddda

Please sign in to comment.