<p>i'm writing this Google Cloud Function (Python)</p>

<pre><code>def create_kubeconfig(request):
    subprocess.check_output("curl https://sdk.cloud.google.com | bash | echo "" ",stdin=subprocess.PIPE, shell=True )
    os.system("./google-cloud-sdk/install.sh")
    os.system("gcloud init")
    os.system("curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.17.0/bin/linux/amd64/kubectl")

    os.system("gcloud container clusters get-credentials **cluster name** --zone us-west2-a --project **project name**")
    os.system("gcloud container clusters get-credentials **cluster name** --zone us-west2-a --project **project name**")
    conf = KubeConfig()
    conf.use_context('**cluster name**')
</code></pre>

<p>when i run the code it gives me the error 
<strong>'Invalid kube-config file. ' kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found.</strong></p>

<p>help me to solve it please</p>

KrisDEV
<p>You have to reach programmatically the K8S API. You have the description of the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17" rel="nofollow noreferrer">API in the documentation</a></p>

<p>But it's not easy and simple to perform. However, here some inputs for achieving what you want.</p>

<p>First, get the GKE master IP
<a href="https://i.stack.imgur.com/IzfVh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IzfVh.png" alt="enter image description here"></a></p>

<p>Then you can access to the cluster easily. Here for reading the deployment</p>

<pre><code>    import google.auth
    from google.auth.transport import requests
    credentials, project_id = google.auth.default()
    session = requests.AuthorizedSession(credentials)
    response = session.get('https://34.76.28.194/apis/apps/v1/namespaces/default/deployments', verify=False)
    response.raise_for_status()
    print(response.json())
</code></pre>

<p>For creating one, you can do this</p>

<pre><code>    import google.auth
    from google.auth.transport import requests
    credentials, project_id = google.auth.default()
    session = requests.AuthorizedSession(credentials)
    with open("deployment.yaml", "r") as f:
        data = f.read()
    response = session.post('https://34.76.28.194/apis/apps/v1/namespaces/default/deployments', data=data,
                            headers={'content-type': 'application/yaml'}, verify=False)
    response.raise_for_status()
    print(response.json())
</code></pre>

<p>According with the object that you want to build, you have to use the correct file definition and the correct API endpoint. I don't know a way to apply a whole <code>yaml</code> with several definition in only one API call.</p>

<p>Last things, be sure to provide the <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/iam" rel="nofollow noreferrer">correct GKE roles</a> to the Cloud Function service Account</p>

<p><strong>UPDATE</strong></p>

<p>Another solution is to use Cloud Run. Indeed, with Cloud Run and thanks to the Container capability, you have the ability to install and to call system process (it's totally open because <a href="https://cloud.google.com/run/docs/reference/container-contract#sandbox" rel="nofollow noreferrer">your container runs into a GVisor sandbox</a>, but most of common usages are allowed)</p>

<p>The idea is the following: use a gcloud SDK base image and deploy your application on it. Then, code your app to perform system calls.</p>

<p>Here a working example in Go</p>

<p>Docker file</p>

<pre><code>FROM golang:1.13 as builder

# Copy local code to the container image.
WORKDIR /app/
COPY go.mod .
ENV GO111MODULE=on
RUN go mod download

COPY . .

# Perform test for building a clean package
RUN go test -v ./...
RUN CGO_ENABLED=0 GOOS=linux go build -v -o server

# Gcloud capable image
FROM google/cloud-sdk

COPY --from=builder /app/server /server
CMD ["/server"]
</code></pre>

<p>Note: The image cloud-sdk image is heavy: 700Mb</p>

<p>The content example (only the happy path. I remove error management, and the stderr/stdout feedback for simplifying the code)</p>

<pre><code>    .......
// Example here: recover the yaml file into a bucket
    client,_ := storage.NewClient(ctx)
    reader,_ := client.Bucket("my_bucket").Object("deployment.yaml").NewReader(ctx)
    content,_:= ioutil.ReadAll(reader)
// You can store locally the file into /tmp directory. It's an in-memory file system. Don't forget to purge it to avoid any out of memory crash
    ioutil.WriteFile("/tmp/file.yaml",content, 0644)
// Execute external command
// 1st Recover the kube authentication
    exec.Command("gcloud","container","clusters","get-credentials","cluster-1","--zone=us-central1-c").Run()
// Then interact with the cluster with kubectl tools and simply apply your description file
    exec.Command("kubectl","apply", "-f","/tmp/file.yaml").Run()
    .......
</code></pre>
