prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  SDK feature request: support soft constraint for scheduling a component to a preemptible node pool
  From a pipeline, it would be useful to have an option to schedule onto a pre-emptible node if such a node pool is available, otherwise fall back to using non-preemptible.
  (Under the hood this can apparently be done with node affinity and `preferredDuringSchedulingIgnoredDuringExecution`).
  
  Ning's suggestion: expose config in the `gcp.use_preemptible_nodepool()` call to specify whether the constraint should be hard or soft.
  
  /assign @gaoning777 (as per discussion)
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `sdk/python/kfp/gcp.py`
  Content:
  ```
  1 # Copyright 2018 Google LLC
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #      http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
  10 # distributed under the License is distributed on an "AS IS" BASIS,
  11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 # See the License for the specific language governing permissions and
  13 # limitations under the License.
  14 
  15 from kubernetes.client import V1Toleration
  16 
  17 def use_gcp_secret(secret_name='user-gcp-sa', secret_file_path_in_volume=None, volume_name=None, secret_volume_mount_path='/secret/gcp-credentials'):
  18     """An operator that configures the container to use GCP service account.
  19 
  20         The user-gcp-sa secret is created as part of the kubeflow deployment that
  21         stores the access token for kubeflow user service account.
  22 
  23         With this service account, the container has a range of GCP APIs to
  24         access to. This service account is automatically created as part of the
  25         kubeflow deployment.
  26 
  27         For the list of the GCP APIs this service account can access to, check
  28         https://github.com/kubeflow/kubeflow/blob/7b0db0d92d65c0746ac52b000cbc290dac7c62b1/deployment/gke/deployment_manager_configs/iam_bindings_template.yaml#L18
  29 
  30         If you want to call the GCP APIs in a different project, grant the kf-user
  31         service account access permission.
  32     """
  33 
  34     # permitted values for secret_name = ['admin-gcp-sa', 'user-gcp-sa']
  35     if secret_file_path_in_volume is None:
  36         secret_file_path_in_volume = '/' + secret_name + '.json'
  37 
  38     if volume_name is None:
  39         volume_name = 'gcp-credentials-' + secret_name
  40 
  41     else:
  42         import warnings
  43         warnings.warn('The volume_name parameter is deprecated and will be removed in next release. The volume names are now generated automatically.', DeprecationWarning)
  44     
  45     def _use_gcp_secret(task):
  46         from kubernetes import client as k8s_client
  47         return (
  48             task
  49                 .add_volume(
  50                     k8s_client.V1Volume(
  51                         name=volume_name,
  52                         secret=k8s_client.V1SecretVolumeSource(
  53                             secret_name=secret_name,
  54                         )
  55                     )
  56                 )
  57                 .add_volume_mount(
  58                     k8s_client.V1VolumeMount(
  59                         name=volume_name,
  60                         mount_path=secret_volume_mount_path,
  61                     )
  62                 )
  63                 .add_env_variable(
  64                     k8s_client.V1EnvVar(
  65                         name='GOOGLE_APPLICATION_CREDENTIALS',
  66                         value=secret_volume_mount_path + secret_file_path_in_volume,
  67                     )
  68                 )
  69                 .add_env_variable(
  70                     k8s_client.V1EnvVar(
  71                         name='CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE',
  72                         value=secret_volume_mount_path + secret_file_path_in_volume,
  73                     )
  74                 ) # Set GCloud Credentials by using the env var override.
  75                   # TODO: Is there a better way for GCloud to pick up the credential?
  76         )
  77     
  78     return _use_gcp_secret
  79 
  80 def use_tpu(tpu_cores: int, tpu_resource: str, tf_version: str):
  81     """An operator that configures GCP TPU spec in a container op.
  82 
  83     Args:
  84       tpu_cores: Required. The number of cores of TPU resource. 
  85         For example, the value can be '8', '32', '128', etc.
  86         Check more details at: https://cloud.google.com/tpu/docs/kubernetes-engine-setup#pod-spec.
  87       tpu_resource: Required. The resource name of the TPU resource. 
  88         For example, the value can be 'v2', 'preemptible-v1', 'v3' or 'preemptible-v3'.
  89         Check more details at: https://cloud.google.com/tpu/docs/kubernetes-engine-setup#pod-spec.
  90       tf_version: Required. The TensorFlow version that the TPU nodes use.
  91         For example, the value can be '1.12', '1.11', '1.9' or '1.8'.
  92         Check more details at: https://cloud.google.com/tpu/docs/supported-versions.
  93     """
  94 
  95     def _set_tpu_spec(task):
  96         task.add_pod_annotation('tf-version.cloud-tpus.google.com', tf_version)
  97         task.add_resource_limit('cloud-tpus.google.com/{}'.format(tpu_resource), str(tpu_cores))
  98         return task
  99 
  100     return _set_tpu_spec
  101 
  102 def use_preemptible_nodepool(toleration: V1Toleration = V1Toleration(effect='NoSchedule',
  103                                                              key='preemptible',
  104                                                              operator='Equal',
  105                                                              value='true')):
  106   """An operator that configures the GKE preemptible in a container op.
  107   """
  108 
  109   def _set_preemptible(task):
  110     task.add_toleration(toleration)
  111     task.add_node_selector_constraint("cloud.google.com/gke-preemptible", "true")
  112     return task
  113 
  114   return _set_preemptible
  115
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/sdk/python/kfp/gcp.py b/sdk/python/kfp/gcp.py
  --- a/sdk/python/kfp/gcp.py
  +++ b/sdk/python/kfp/gcp.py
  @@ -12,7 +12,8 @@
   # See the License for the specific language governing permissions and
   # limitations under the License.
   
  -from kubernetes.client import V1Toleration
  +from kubernetes.client import V1Toleration, V1Affinity, V1NodeAffinity, \
  +  V1NodeSelector, V1NodeSelectorTerm, V1NodeSelectorRequirement, V1PreferredSchedulingTerm
   
   def use_gcp_secret(secret_name='user-gcp-sa', secret_file_path_in_volume=None, volume_name=None, secret_volume_mount_path='/secret/gcp-credentials'):
       """An operator that configures the container to use GCP service account.
  @@ -102,13 +103,31 @@
   def use_preemptible_nodepool(toleration: V1Toleration = V1Toleration(effect='NoSchedule',
                                                                key='preemptible',
                                                                operator='Equal',
  -                                                             value='true')):
  +                                                             value='true'),
  +                              hard_constraint: bool = False):
     """An operator that configures the GKE preemptible in a container op.
  +  Args:
  +    toleration (V1Toleration): toleration to pods, default is the preemptible label.
  +    hard_constraint (bool): the constraint of scheduling the pods on preemptible
  +        nodepools is hard. (Default: False)
     """
   
     def _set_preemptible(task):
       task.add_toleration(toleration)
  -    task.add_node_selector_constraint("cloud.google.com/gke-preemptible", "true")
  +    node_selector_term = V1NodeSelectorTerm(match_expressions=[
  +        V1NodeSelectorRequirement(key='cloud.google.com/gke-preemptible',
  +                                  operator='In',
  +                                  values=['true'])]
  +    )
  +    if hard_constraint:
  +      node_affinity = V1NodeAffinity(required_during_scheduling_ignored_during_execution=
  +                        V1NodeSelector(node_selector_terms=[node_selector_term]))
  +    else:
  +      node_affinity = V1NodeAffinity(preferred_during_scheduling_ignored_during_execution=
  +                        V1PreferredSchedulingTerm(preference=node_selector_term,
  +                                                  weight=50))
  +    affinity = V1Affinity(node_affinity=node_affinity)
  +    task.add_affinity(affinity=affinity)
       return task
   
     return _set_preemptible
  
verification_info: {"golden_diff": "diff --git a/sdk/python/kfp/gcp.py b/sdk/python/kfp/gcp.py\n--- a/sdk/python/kfp/gcp.py\n+++ b/sdk/python/kfp/gcp.py\n@@ -12,7 +12,8 @@\n # See the License for the specific language governing permissions and\n # limitations under the License.\n \n-from kubernetes.client import V1Toleration\n+from kubernetes.client import V1Toleration, V1Affinity, V1NodeAffinity, \\\n+  V1NodeSelector, V1NodeSelectorTerm, V1NodeSelectorRequirement, V1PreferredSchedulingTerm\n \n def use_gcp_secret(secret_name='user-gcp-sa', secret_file_path_in_volume=None, volume_name=None, secret_volume_mount_path='/secret/gcp-credentials'):\n     \"\"\"An operator that configures the container to use GCP service account.\n@@ -102,13 +103,31 @@\n def use_preemptible_nodepool(toleration: V1Toleration = V1Toleration(effect='NoSchedule',\n                                                              key='preemptible',\n                                                              operator='Equal',\n-                                                             value='true')):\n+                                                             value='true'),\n+                              hard_constraint: bool = False):\n   \"\"\"An operator that configures the GKE preemptible in a container op.\n+  Args:\n+    toleration (V1Toleration): toleration to pods, default is the preemptible label.\n+    hard_constraint (bool): the constraint of scheduling the pods on preemptible\n+        nodepools is hard. (Default: False)\n   \"\"\"\n \n   def _set_preemptible(task):\n     task.add_toleration(toleration)\n-    task.add_node_selector_constraint(\"cloud.google.com/gke-preemptible\", \"true\")\n+    node_selector_term = V1NodeSelectorTerm(match_expressions=[\n+        V1NodeSelectorRequirement(key='cloud.google.com/gke-preemptible',\n+                                  operator='In',\n+                                  values=['true'])]\n+    )\n+    if hard_constraint:\n+      node_affinity = V1NodeAffinity(required_during_scheduling_ignored_during_execution=\n+                        V1NodeSelector(node_selector_terms=[node_selector_term]))\n+    else:\n+      node_affinity = V1NodeAffinity(preferred_during_scheduling_ignored_during_execution=\n+                        V1PreferredSchedulingTerm(preference=node_selector_term,\n+                                                  weight=50))\n+    affinity = V1Affinity(node_affinity=node_affinity)\n+    task.add_affinity(affinity=affinity)\n     return task\n \n   return _set_preemptible\n", "issue": "SDK feature request: support soft constraint for scheduling a component to a preemptible node pool\nFrom a pipeline, it would be useful to have an option to schedule onto a pre-emptible node if such a node pool is available, otherwise fall back to using non-preemptible.\r\n(Under the hood this can apparently be done with node affinity and `preferredDuringSchedulingIgnoredDuringExecution`).\r\n\r\nNing's suggestion: expose config in the `gcp.use_preemptible_nodepool()` call to specify whether the constraint should be hard or soft.\r\n\r\n/assign @gaoning777 (as per discussion)\n", "before_files": [{"content": "# Copyright 2018 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom kubernetes.client import V1Toleration\n\ndef use_gcp_secret(secret_name='user-gcp-sa', secret_file_path_in_volume=None, volume_name=None, secret_volume_mount_path='/secret/gcp-credentials'):\n    \"\"\"An operator that configures the container to use GCP service account.\n\n        The user-gcp-sa secret is created as part of the kubeflow deployment that\n        stores the access token for kubeflow user service account.\n\n        With this service account, the container has a range of GCP APIs to\n        access to. This service account is automatically created as part of the\n        kubeflow deployment.\n\n        For the list of the GCP APIs this service account can access to, check\n        https://github.com/kubeflow/kubeflow/blob/7b0db0d92d65c0746ac52b000cbc290dac7c62b1/deployment/gke/deployment_manager_configs/iam_bindings_template.yaml#L18\n\n        If you want to call the GCP APIs in a different project, grant the kf-user\n        service account access permission.\n    \"\"\"\n\n    # permitted values for secret_name = ['admin-gcp-sa', 'user-gcp-sa']\n    if secret_file_path_in_volume is None:\n        secret_file_path_in_volume = '/' + secret_name + '.json'\n\n    if volume_name is None:\n        volume_name = 'gcp-credentials-' + secret_name\n\n    else:\n        import warnings\n        warnings.warn('The volume_name parameter is deprecated and will be removed in next release. The volume names are now generated automatically.', DeprecationWarning)\n    \n    def _use_gcp_secret(task):\n        from kubernetes import client as k8s_client\n        return (\n            task\n                .add_volume(\n                    k8s_client.V1Volume(\n                        name=volume_name,\n                        secret=k8s_client.V1SecretVolumeSource(\n                            secret_name=secret_name,\n                        )\n                    )\n                )\n                .add_volume_mount(\n                    k8s_client.V1VolumeMount(\n                        name=volume_name,\n                        mount_path=secret_volume_mount_path,\n                    )\n                )\n                .add_env_variable(\n                    k8s_client.V1EnvVar(\n                        name='GOOGLE_APPLICATION_CREDENTIALS',\n                        value=secret_volume_mount_path + secret_file_path_in_volume,\n                    )\n                )\n                .add_env_variable(\n                    k8s_client.V1EnvVar(\n                        name='CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE',\n                        value=secret_volume_mount_path + secret_file_path_in_volume,\n                    )\n                ) # Set GCloud Credentials by using the env var override.\n                  # TODO: Is there a better way for GCloud to pick up the credential?\n        )\n    \n    return _use_gcp_secret\n\ndef use_tpu(tpu_cores: int, tpu_resource: str, tf_version: str):\n    \"\"\"An operator that configures GCP TPU spec in a container op.\n\n    Args:\n      tpu_cores: Required. The number of cores of TPU resource. \n        For example, the value can be '8', '32', '128', etc.\n        Check more details at: https://cloud.google.com/tpu/docs/kubernetes-engine-setup#pod-spec.\n      tpu_resource: Required. The resource name of the TPU resource. \n        For example, the value can be 'v2', 'preemptible-v1', 'v3' or 'preemptible-v3'.\n        Check more details at: https://cloud.google.com/tpu/docs/kubernetes-engine-setup#pod-spec.\n      tf_version: Required. The TensorFlow version that the TPU nodes use.\n        For example, the value can be '1.12', '1.11', '1.9' or '1.8'.\n        Check more details at: https://cloud.google.com/tpu/docs/supported-versions.\n    \"\"\"\n\n    def _set_tpu_spec(task):\n        task.add_pod_annotation('tf-version.cloud-tpus.google.com', tf_version)\n        task.add_resource_limit('cloud-tpus.google.com/{}'.format(tpu_resource), str(tpu_cores))\n        return task\n\n    return _set_tpu_spec\n\ndef use_preemptible_nodepool(toleration: V1Toleration = V1Toleration(effect='NoSchedule',\n                                                             key='preemptible',\n                                                             operator='Equal',\n                                                             value='true')):\n  \"\"\"An operator that configures the GKE preemptible in a container op.\n  \"\"\"\n\n  def _set_preemptible(task):\n    task.add_toleration(toleration)\n    task.add_node_selector_constraint(\"cloud.google.com/gke-preemptible\", \"true\")\n    return task\n\n  return _set_preemptible\n", "path": "sdk/python/kfp/gcp.py"}], "after_files": [{"content": "# Copyright 2018 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom kubernetes.client import V1Toleration, V1Affinity, V1NodeAffinity, \\\n  V1NodeSelector, V1NodeSelectorTerm, V1NodeSelectorRequirement, V1PreferredSchedulingTerm\n\ndef use_gcp_secret(secret_name='user-gcp-sa', secret_file_path_in_volume=None, volume_name=None, secret_volume_mount_path='/secret/gcp-credentials'):\n    \"\"\"An operator that configures the container to use GCP service account.\n\n        The user-gcp-sa secret is created as part of the kubeflow deployment that\n        stores the access token for kubeflow user service account.\n\n        With this service account, the container has a range of GCP APIs to\n        access to. This service account is automatically created as part of the\n        kubeflow deployment.\n\n        For the list of the GCP APIs this service account can access to, check\n        https://github.com/kubeflow/kubeflow/blob/7b0db0d92d65c0746ac52b000cbc290dac7c62b1/deployment/gke/deployment_manager_configs/iam_bindings_template.yaml#L18\n\n        If you want to call the GCP APIs in a different project, grant the kf-user\n        service account access permission.\n    \"\"\"\n\n    # permitted values for secret_name = ['admin-gcp-sa', 'user-gcp-sa']\n    if secret_file_path_in_volume is None:\n        secret_file_path_in_volume = '/' + secret_name + '.json'\n\n    if volume_name is None:\n        volume_name = 'gcp-credentials-' + secret_name\n\n    else:\n        import warnings\n        warnings.warn('The volume_name parameter is deprecated and will be removed in next release. The volume names are now generated automatically.', DeprecationWarning)\n    \n    def _use_gcp_secret(task):\n        from kubernetes import client as k8s_client\n        return (\n            task\n                .add_volume(\n                    k8s_client.V1Volume(\n                        name=volume_name,\n                        secret=k8s_client.V1SecretVolumeSource(\n                            secret_name=secret_name,\n                        )\n                    )\n                )\n                .add_volume_mount(\n                    k8s_client.V1VolumeMount(\n                        name=volume_name,\n                        mount_path=secret_volume_mount_path,\n                    )\n                )\n                .add_env_variable(\n                    k8s_client.V1EnvVar(\n                        name='GOOGLE_APPLICATION_CREDENTIALS',\n                        value=secret_volume_mount_path + secret_file_path_in_volume,\n                    )\n                )\n                .add_env_variable(\n                    k8s_client.V1EnvVar(\n                        name='CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE',\n                        value=secret_volume_mount_path + secret_file_path_in_volume,\n                    )\n                ) # Set GCloud Credentials by using the env var override.\n                  # TODO: Is there a better way for GCloud to pick up the credential?\n        )\n    \n    return _use_gcp_secret\n\ndef use_tpu(tpu_cores: int, tpu_resource: str, tf_version: str):\n    \"\"\"An operator that configures GCP TPU spec in a container op.\n\n    Args:\n      tpu_cores: Required. The number of cores of TPU resource. \n        For example, the value can be '8', '32', '128', etc.\n        Check more details at: https://cloud.google.com/tpu/docs/kubernetes-engine-setup#pod-spec.\n      tpu_resource: Required. The resource name of the TPU resource. \n        For example, the value can be 'v2', 'preemptible-v1', 'v3' or 'preemptible-v3'.\n        Check more details at: https://cloud.google.com/tpu/docs/kubernetes-engine-setup#pod-spec.\n      tf_version: Required. The TensorFlow version that the TPU nodes use.\n        For example, the value can be '1.12', '1.11', '1.9' or '1.8'.\n        Check more details at: https://cloud.google.com/tpu/docs/supported-versions.\n    \"\"\"\n\n    def _set_tpu_spec(task):\n        task.add_pod_annotation('tf-version.cloud-tpus.google.com', tf_version)\n        task.add_resource_limit('cloud-tpus.google.com/{}'.format(tpu_resource), str(tpu_cores))\n        return task\n\n    return _set_tpu_spec\n\ndef use_preemptible_nodepool(toleration: V1Toleration = V1Toleration(effect='NoSchedule',\n                                                             key='preemptible',\n                                                             operator='Equal',\n                                                             value='true'),\n                              hard_constraint: bool = False):\n  \"\"\"An operator that configures the GKE preemptible in a container op.\n  Args:\n    toleration (V1Toleration): toleration to pods, default is the preemptible label.\n    hard_constraint (bool): the constraint of scheduling the pods on preemptible\n        nodepools is hard. (Default: False)\n  \"\"\"\n\n  def _set_preemptible(task):\n    task.add_toleration(toleration)\n    node_selector_term = V1NodeSelectorTerm(match_expressions=[\n        V1NodeSelectorRequirement(key='cloud.google.com/gke-preemptible',\n                                  operator='In',\n                                  values=['true'])]\n    )\n    if hard_constraint:\n      node_affinity = V1NodeAffinity(required_during_scheduling_ignored_during_execution=\n                        V1NodeSelector(node_selector_terms=[node_selector_term]))\n    else:\n      node_affinity = V1NodeAffinity(preferred_during_scheduling_ignored_during_execution=\n                        V1PreferredSchedulingTerm(preference=node_selector_term,\n                                                  weight=50))\n    affinity = V1Affinity(node_affinity=node_affinity)\n    task.add_affinity(affinity=affinity)\n    return task\n\n  return _set_preemptible\n", "path": "sdk/python/kfp/gcp.py"}]}