text: # Licensed to the Apache Software Foundation (ASF) under one
  # or more contributor license agreements.  See the NOTICE file
  # distributed with this work for additional information
  # regarding copyright ownership.  The ASF licenses this file
  # to you under the Apache License, Version 2.0 (the
  # "License"); you may not use this file except in compliance
  # with the License.  You may obtain a copy of the License at
  #
  #   http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing,
  # software distributed under the License is distributed on an
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  # KIND, either express or implied.  See the License for the
  # specific language governing permissions and limitations
  # under the License.
  #
  import kopf
  import os, os.path
  import subprocess
  import logging
  
  def patch(n):
    return f"""apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: demo-deploy
    namespace: demo
  spec:
    replicas: {n}
  """
  
  def kubectl(cmd, patch):
    print(f"kubectl {cmd}")
    with open(f"deploy/patch.yaml", "w") as f:
      f.write(patch)
    res = subprocess.run(["kubectl", cmd, "-k", "deploy"], capture_output=True)
    if res.returncode == 0:
      return res.stdout.decode()
    return res.stderr.decode()
  
  @kopf.on.login()
  def sample_login(**kwargs):
      token = '/var/run/secrets/kubernetes.io/serviceaccount/token'
      if os.path.isfile(token):
          logging.debug("found serviceaccount token: login via pykube in kubernetes")
          return kopf.login_with_service_account(**kwargs) 
      logging.debug("login via client")
      return  kopf.login_with_kubeconfig(**kwargs)
  
  @kopf.on.create('nuvolaris.org', 'v1', 'samples')
  def sample_create(spec, **kwargs):
      count = spec["count"]
      message = kubectl("apply", patch(count))
      return { "message": message }
  
  @kopf.on.delete('nuvolaris.org', 'v1', 'samples')
  def sample_delete(spec, **kwargs):
      count = spec["count"]
      message = kubectl("delete", patch(count))
      return { "message": "delete" }
  