allansun/kubernetes-php-client

add read log query parameters

Closed this issue ยท 7 comments

/**
     * read log of the specified Pod
     *
     * @param $namespace
     * @param $name
     * @return string|mixed
     */
    public function readLog($namespace = 'default', $name)
    {
        return $this->parseResponse(
        	$this->client->request('get',
        		"/api/v1/namespaces/{$namespace}/pods/{$name}/log"
        		,[
        		]
        	)
        	, 'readCoreV1NamespacedPodLog'
        );
    }

Kubernetes\API\Pod class, need to add query parameter to readLog function as third parameter.

there are query parameters in Kubernetes API Reference : https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12

I faced the same problem very recently. I needed to pass container query param. For a full list of possible params check here. My temporary workaround was to extend the Pod:class, create a new function and pass the query param I needed. My proposition though is to solve it properly, by using the guzzle ability to pass query params. So an example implementation could be

/**
     * read log of the specified Pod
     *
     * @param $namespace
     * @param $name
     * @return string|mixed
     */
    public function readLog(string $namespace = 'default',string  $name, array $params =[])
    {
        return $this->parseResponse(
        	$this->client->request('get',
        		"/api/v1/namespaces/{$namespace}/pods/{$name}/log"
        		,[
                            'query' => $params
        		]
        	)
        	, 'readCoreV1NamespacedPodLog'
        );
    } 

Dear @allansun, if you are open to contributions let me know to raise a PR that will propose this change.

@stafot Thanks for bring this up. PR is welcome!

Could you please provide some instructions on how to pull swagger.json after we pull master branch because it is not included. Do you have a manual flow for backward releases? If yes could you describe the steps to find a way to automate it? Thanks in advance, @allansun

@stafot @Veitor Thanks for the long waiting, I have 'almost' fully refactored the code generation logic for API part, all 'parameters' as described in the swagger files are correctly parsed and implemented into the generated code.

Your requested $queries can not be injected into the request

public function readLog(string $namespace, string $name, array $queries = [])
{
return $this->parseResponse(
$this->client->request('get',
"/api/v1/namespaces/{$namespace}/pods/{$name}/log",
[
'query' => $queries,
]
),
'readCoreV1NamespacedPodLog'
);
}

If you noticed I've also managed to put the accepted parameters as PHPDoc, so you don't always have to go through to the Kubernetes documentation, a validator maybe implemented in future release, but it think at least it works now.

/**
* read log of the specified Pod
*
* @param string $namespace object name and auth scope, such as for teams and
* projects
* @param string $name name of the Pod
* @param array $queries options:
* 'container' string
* The container for which to stream logs. Defaults to only container if there is
* one container in the pod.
* 'follow' boolean
* Follow the log stream of the pod. Defaults to false.
* 'limitBytes' integer
* If set, the number of bytes to read from the server before terminating the log
* output. This may not display a complete final line of logging, and may return
* slightly more or slightly less than the specified limit.
* 'pretty' string
* If 'true', then the output is pretty printed.
* 'previous' boolean
* Return previous terminated container logs. Defaults to false.
* 'sinceSeconds' integer
* A relative time in seconds before the current time from which to show logs. If
* this value precedes the time a pod was started, only logs since the pod start
* will be returned. If this value is in the future, no logs will be returned. Only
* one of sinceSeconds or sinceTime may be specified.
* 'tailLines' integer
* If set, the number of lines from the end of the logs to show. If not specified,
* logs are shown from the creation of the container or sinceSeconds or sinceTime
* 'timestamps' boolean
* If true, add an RFC3339 or RFC3339Nano timestamp at the beginning of every line
* of log output. Defaults to false.
*
* @return string|mixed
*/
public function readLog(string $namespace, string $name, array $queries = [])
{

Please notice I didn't re-tag the releases, instead I created a patch to the latest stable release of each major version, you can reference to those specific composer version for the latest generated code. All new code generated from now on will follow the new code template.

xc060 10-04 20-36-56

7z0eq 10-04 20-49-43

Great @allansun, I will try it tomorrow and let you know. Thanks for addressed this.

Worked in my case!