func()

in pkg/cmd/log.go [70:212]


func (o *logCmdOptions) run(cmd *cobra.Command, args []string) error {
	c, err := o.GetCmdClient()
	if err != nil {
		return err
	}

	integrationID := args[0]

	integration := v1.Integration{
		TypeMeta: metav1.TypeMeta{
			Kind:       v1.IntegrationKind,
			APIVersion: v1.SchemeGroupVersion.String(),
		},
		ObjectMeta: metav1.ObjectMeta{
			Namespace: o.Namespace,
			Name:      integrationID,
		},
	}
	key := k8sclient.ObjectKey{
		Namespace: o.Namespace,
		Name:      integrationID,
	}

	pollTimeout := 600 * time.Second // 10 minutes should be adequate for a timeout
	pollInterval := 2 * time.Second
	currLogMsg := ""
	newLogMsg := ""

	// wrong deprecation notice --> https://github.com/kubernetes/apimachinery/issues/153
	//nolint:staticcheck
	err = wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
		//
		// Reduce repetition of messages by tracking the last message
		// and checking if its different from the new message
		//
		if newLogMsg != currLogMsg {
			fmt.Fprintln(cmd.OutOrStdout(), newLogMsg)
			currLogMsg = newLogMsg
		}

		//
		// Try and find the integration
		//
		err = c.Get(o.Context, key, &integration)
		if err != nil && !k8errors.IsNotFound(err) {
			// different error so return
			return false, err
		}

		if k8errors.IsNotFound(err) {
			//
			// Don't have an integration yet so log and wait
			//
			newLogMsg = fmt.Sprintf("Integration '%s' not yet available. Will keep checking ...", integrationID)
			return false, nil
		}

		//
		// Found the integration so check its status using its phase
		//
		phase := integration.Status.Phase
		switch phase {
		case "Running":
			//
			// Found the running integration so step over to scraping its pod log
			//
			fmt.Fprintln(cmd.OutOrStdout(), "Integration '"+integrationID+"' is now running. Showing log ...")
			var tailLines *int64
			if o.Tail > 0 {
				tailLines = &o.Tail
			}
			if err := k8slog.Print(o.Context, cmd, c, &integration, tailLines, cmd.OutOrStdout()); err != nil {
				return false, err
			}

			return true, nil
		case "Building Kit":
			//
			// This phase can take a while so check progress using
			// the associated Integration Kit's progress
			//
			newLogMsg = fmt.Sprintf("The building kit for integration '%s' is being initialised. This may take some time ...", integrationID)
			if integration.Status.IntegrationKit == nil {
				//
				// Not created yet so wait quietly
				//
				return false, nil
			}

			integrationKit := v1.IntegrationKit{
				TypeMeta: metav1.TypeMeta{
					Kind:       v1.IntegrationKitKind,
					APIVersion: v1.SchemeGroupVersion.String(),
				},
				ObjectMeta: metav1.ObjectMeta{
					Namespace: integration.Status.IntegrationKit.Namespace,
					Name:      integration.Status.IntegrationKit.Name,
				},
			}
			ikKey := k8sclient.ObjectKey{
				Namespace: integration.Status.IntegrationKit.Namespace,
				Name:      integration.Status.IntegrationKit.Name,
			}

			//
			// Query for the integration kit
			//
			if err := c.Get(o.Context, ikKey, &integrationKit); err != nil {
				if !k8errors.IsNotFound(err) {
					//
					// Not created yet so wait quietly
					//
					return false, nil
				}
				//
				// Integration kit query made an error
				//
				return false, err
			}

			//
			// Found the building kit so output its phase
			//
			newLogMsg = fmt.Sprintf("The building kit for integration '%s' is at: %s", integrationID, integrationKit.Status.Phase)
		default:
			//
			// Integration is still building, deploying or even in error
			//
			newLogMsg = fmt.Sprintf("Integration '%s' is at: %s ...", integrationID, phase)
		}

		return false, nil
	})

	if err != nil {
		return err
	}

	// Let's add a Wait point, otherwise the script terminates
	<-o.Context.Done()

	return nil
}