StartupWorkerFor copy kubeConfig and start ttyd
(ctx context.Context, cloudshell *cloudshellv1alpha1.CloudShell)
| 399 | |
| 400 | // StartupWorkerFor copy kubeConfig and start ttyd |
| 401 | func (c *Controller) StartupWorkerFor(ctx context.Context, cloudshell *cloudshellv1alpha1.CloudShell) error { |
| 402 | // if secretRef is empty, use the Incluster rest config to generate kubeconfig and restore a secret. |
| 403 | // the kubeconfig only work on current cluster. |
| 404 | var ( |
| 405 | kubeConfigByte []byte |
| 406 | err error |
| 407 | ) |
| 408 | if cloudshell.Spec.SecretRef == nil { |
| 409 | kubeConfigByte, err = GenerateKubeconfigInCluster() |
| 410 | if err != nil { |
| 411 | return err |
| 412 | } |
| 413 | |
| 414 | secret := &corev1.Secret{ |
| 415 | ObjectMeta: metav1.ObjectMeta{ |
| 416 | GenerateName: fmt.Sprintf("cloudshell-%s-", cloudshell.Name), |
| 417 | Namespace: cloudshell.Namespace, |
| 418 | }, |
| 419 | Data: map[string][]byte{"config": kubeConfigByte}, |
| 420 | } |
| 421 | |
| 422 | if err := ctrlutil.SetControllerReference(cloudshell, secret, c.Scheme); err != nil { |
| 423 | klog.ErrorS(err, "Failed to set owner reference for configmap", "cloudshell", klog.KObj(cloudshell)) |
| 424 | return err |
| 425 | } |
| 426 | if err := c.Client.Create(ctx, secret); err != nil { |
| 427 | return err |
| 428 | } |
| 429 | cloudshell.Spec.SecretRef = &cloudshellv1alpha1.LocalSecretReference{ |
| 430 | Name: secret.Name, |
| 431 | } |
| 432 | if err := c.Update(ctx, cloudshell); err != nil { |
| 433 | return err |
| 434 | } |
| 435 | } else { |
| 436 | secret := &corev1.Secret{} |
| 437 | err := c.Client.Get(ctx, client.ObjectKey{Namespace: cloudshell.Namespace, Name: cloudshell.Spec.SecretRef.Name}, secret) |
| 438 | if err != nil { |
| 439 | return err |
| 440 | } |
| 441 | |
| 442 | configDataName := os.Getenv("KUBECONFIG_SECRET_DATA_NAME") |
| 443 | if configDataName == "" { |
| 444 | configDataName = "config" |
| 445 | } |
| 446 | kubeConfigByte = secret.Data[configDataName] |
| 447 | } |
| 448 | |
| 449 | return c.StartupWorker(ctx, cloudshell, kubeConfigByte) |
| 450 | } |
| 451 | |
| 452 | func (c *Controller) StartupWorker(_ context.Context, cloudshell *cloudshellv1alpha1.CloudShell, kubeConfigByte []byte) error { |
| 453 | // TODO: Some extra logic in order to upload and download files. |
no test coverage detected