Example #1
0
def DeleteConnectNamespace(kube_client, args):
    """Delete the namespace in the cluster that contains the connect agent.

  Args:
    kube_client: A Kubernetes Client for the cluster to be registered.
    args: an argparse namespace. All arguments that were provided to this
      command invocation.

  Raises:
    calliope_exceptions.MinimumArgumentException: if a kubeconfig file cannot
      be deduced from the command line flags or environment
  """

    namespaces = _GKEConnectNamespace(
        kube_client, properties.VALUES.core.project.GetOrFail())

    if len(namespaces) > 1:
        log.warning(
            'gcloud will not remove any namespaces containing the Connect Agent since'
            ' it was found running in multiple namespaces on cluster: [{}].'
            ' Please delete these namespaces [{}] maually in your cluster'.
            format(args.CLUSTER_NAME, namespaces))
        return

    namespace = namespaces[0]
    cleanup_msg = 'Please delete namespace [{}] manually in your cluster.'.format(
        namespace)

    try:
        kube_util.DeleteNamespace(kube_client, namespace)
    except exceptions.Error:
        log.warning(cleanup_msg)
Example #2
0
def DeployConnectAgent(kube_client,
                       args,
                       service_account_key_data,
                       image_pull_secret_data,
                       membership_ref,
                       release_track=None):
    """Deploys the Connect Agent to the cluster.

  Args:
    kube_client: A Kubernetes Client for the cluster to be registered.
    args: arguments of the command.
    service_account_key_data: The contents of a Google IAM service account JSON
      file
    image_pull_secret_data: The contents of image pull secret to use for
      private registries.
    membership_ref: The membership should be associated with the connect agent
      in the format of
      `project/[PROJECT]/location/global/memberships/[MEMBERSHIP]`.
    release_track: the release_track used in the gcloud command,
      or None if it is not available.
  Raises:
    exceptions.Error: If the agent cannot be deployed properly
    calliope_exceptions.MinimumArgumentException: If the agent cannot be
    deployed properly
  """
    project_id = properties.VALUES.core.project.GetOrFail()

    log.status.Print('Generating the Connect Agent manifest...')
    full_manifest = _GenerateManifest(args, service_account_key_data,
                                      image_pull_secret_data, False,
                                      membership_ref, release_track)

    # Generate a manifest file if necessary.
    if args.manifest_output_file:
        try:
            files.WriteFileContents(files.ExpandHomeDir(
                args.manifest_output_file),
                                    full_manifest,
                                    private=True)
        except files.Error as e:
            raise exceptions.Error(
                'Could not create manifest file: {}'.format(e))

        log.status.Print(
            MANIFEST_SAVED_MESSAGE.format(args.manifest_output_file))
        return

    namespaces = _GKEConnectNamespace(kube_client, project_id)
    if len(namespaces) > 1:
        raise exceptions.Error(
            'Multiple namespaces [{}] containing the Connect Agent found in'
            'cluster [{}]. Cannot deploy a new Connect Agent'.format(
                namespaces, args.CLUSTER_NAME))
    namespace = namespaces[0]

    log.status.Print(
        'Deploying the Connect Agent on cluster [{}] in namespace [{}]...'.
        format(args.CLUSTER_NAME, namespace))
    # Delete the ns if necessary
    kube_util.DeleteNamespace(kube_client, namespace)

    # TODO(b/138816749): add check for cluster-admin permissions
    _PurgeAlphaInstaller(kube_client, namespace, project_id)
    # # Create or update the agent install deployment and related resources.
    _, err = kube_client.Apply(full_manifest)
    if err:
        raise exceptions.Error(
            'Failed to apply manifest to cluster: {}'.format(err))
    # TODO(b/131925085): Check connect agent health status.
    log.status.Print(
        'Deployed the Connect Agent on cluster [{}] in namespace [{}].'.format(
            args.CLUSTER_NAME, namespace))