Пример #1
0
 public void Deserialize()
 {
     {
         var q = KubernetesJson.Deserialize <ResourceQuantity>("\"12k\"");
         Assert.Equal(new ResourceQuantity(12000, 0, DecimalSI), q);
     }
 }
Пример #2
0
        /// <summary>
        /// Patch kubernetes object.
        /// </summary>
        /// <typeparam name="T">the object type</typeparam>
        /// <param name="namespaceProperty"> the namespaceProperty</param>
        /// <param name="name"> the name</param>
        /// <param name="obj"> the object</param>
        /// <param name="patchOptions">the patch options</param>
        /// <param name="cancellationToken">the token </param>
        /// <returns>the kubernetes object</returns>
        public async Task <T> PatchAsync <T>(string namespaceProperty, string name, object obj, PatchOptions patchOptions, CancellationToken cancellationToken = default)
            where T : class, IKubernetesObject <V1ObjectMeta>
        {
            if (string.IsNullOrEmpty(namespaceProperty))
            {
                throw new ArgumentNullException(nameof(namespaceProperty));
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (patchOptions == null)
            {
                throw new ArgumentNullException(nameof(patchOptions));
            }

            var resp = await _client.PatchNamespacedCustomObjectWithHttpMessagesAsync(body : obj, group : _apiGroup, version : _apiVersion, namespaceParameter : namespaceProperty, plural : _resourcePlural,
                                                                                      name : name, dryRun : patchOptions.DryRun, fieldManager : patchOptions.FieldManager, force : patchOptions.Force, cancellationToken : cancellationToken).ConfigureAwait(false);

            return(KubernetesJson.Deserialize <T>(resp.Body.ToString()));
        }
Пример #3
0
        /// <summary>
        /// Create kubernetes object.
        /// </summary>
        /// <typeparam name="T">the object type</typeparam>
        /// <param name="obj">the object</param>
        /// <param name="createOptions">the create options</param>
        /// <param name="cancellationToken">the token </param>
        /// <returns>the kubernetes object</returns>
        public async Task <T> CreateAsync <T>(T obj, CreateOptions createOptions, CancellationToken cancellationToken = default)
            where T : class, IKubernetesObject <V1ObjectMeta>
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (createOptions == null)
            {
                throw new ArgumentNullException(nameof(createOptions));
            }

            V1ObjectMeta objectMeta = obj.Metadata;

            var isNamespaced = !string.IsNullOrEmpty(objectMeta.NamespaceProperty);

            if (isNamespaced)
            {
                return(await CreateAsync(objectMeta.NamespaceProperty, obj, createOptions, cancellationToken).ConfigureAwait(false));
            }

            var resp = await _client.CreateClusterCustomObjectWithHttpMessagesAsync(body : obj, group : _apiGroup, plural : _resourcePlural, version : _apiVersion, dryRun : createOptions.DryRun,
                                                                                    fieldManager : createOptions.FieldManager, cancellationToken : cancellationToken).ConfigureAwait(false);

            return(KubernetesJson.Deserialize <T>(resp.Body.ToString()));
        }
Пример #4
0
        /// <summary>
        /// Update status of kubernetes object.
        /// </summary>
        /// <typeparam name="T">the object type</typeparam>
        /// <param name="obj"> the object</param>
        /// <param name="status"> function to extract the status from the object</param>
        /// <param name="updateOptions">the update options</param>
        /// <param name="cancellationToken">the token </param>
        /// <returns>the kubernetes object</returns>
        public async Task <T> UpdateStatusAsync <T>(T obj, Func <T, object> status, UpdateOptions updateOptions, CancellationToken cancellationToken = default)
            where T : class, IKubernetesObject <V1ObjectMeta>
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (updateOptions == null)
            {
                throw new ArgumentNullException(nameof(updateOptions));
            }

            V1ObjectMeta objectMeta = obj.Metadata;
            HttpOperationResponse <object> resp;
            var isNamespaced = !string.IsNullOrEmpty(objectMeta.NamespaceProperty);

            if (isNamespaced)
            {
                resp = await _client.PatchNamespacedCustomObjectStatusWithHttpMessagesAsync(body : obj, group : _apiGroup, version : _apiVersion, namespaceParameter : objectMeta.NamespaceProperty,
                                                                                            plural : _resourcePlural, name : objectMeta.Name, dryRun : updateOptions.DryRun, fieldManager : updateOptions.FieldManager, force : updateOptions.Force,
                                                                                            cancellationToken : cancellationToken).ConfigureAwait(false);
            }
            else
            {
                resp = await _client.PatchClusterCustomObjectStatusWithHttpMessagesAsync(body : obj, group : _apiGroup, version : _apiVersion, plural : _resourcePlural, name : objectMeta.Name,
                                                                                         dryRun : updateOptions.DryRun, fieldManager : updateOptions.FieldManager, force : updateOptions.Force, cancellationToken : cancellationToken).ConfigureAwait(false);
            }

            return(KubernetesJson.Deserialize <T>(resp.Body.ToString()));
        }
Пример #5
0
        public void ReturnObject()
        {
            var corev1Namespace = new V1Namespace()
            {
                Metadata = new V1ObjectMeta()
                {
                    Name = "test name"
                },
                Status = new V1NamespaceStatus()
                {
                    Phase = "test termating"
                },
            };

            using (var server = new MockKubeApiServer(testOutput, resp: KubernetesJson.Serialize(corev1Namespace)))
            {
                var client = new Kubernetes(new KubernetesClientConfiguration {
                    Host = server.Uri.ToString()
                });

                var status = client.DeleteNamespace("test", new V1DeleteOptions());

                Assert.True(status.HasObject);

                var obj = status.ObjectView <V1Namespace>();

                Assert.Equal(obj.Metadata.Name, corev1Namespace.Metadata.Name);
                Assert.Equal(obj.Status.Phase, corev1Namespace.Status.Phase);
            }
        }
        private static string BuildWatchEventStreamLine(WatchEventType eventType)
        {
            var corev1PodList = KubernetesJson.Deserialize <V1PodList>(MockPodResponse);

            return(KubernetesJson.Serialize(new Watcher <V1Pod> .WatchEvent {
                Type = eventType, Object = corev1PodList.Items.First()
            }));
        }
Пример #7
0
    private static T Deserialize <T>(JsonElement element)
    {
        // If we were able to get the default options used by KubernetesJson,
        // deserialize directly, it's going to be faster.
        if (KubernetesJsonOptions.DefaultOptions is { } options)
        {
            element.Deserialize <T>(options);
        }

        // Else, fallback to the slower but more reliable method.
        return(KubernetesJson.Deserialize <T>(element.GetRawText()));
    }
Пример #8
0
        public void Deserialize()
        {
            {
                var v = KubernetesJson.Deserialize <IntstrIntOrString>("1234");
                Assert.Equal("1234", v.Value);
            }

            {
                var v = KubernetesJson.Deserialize <IntstrIntOrString>("\"12%\"");
                Assert.Equal("12%", v.Value);
            }
        }
Пример #9
0
        /// <summary>
        /// Delete kubernetes object.
        /// </summary>
        /// <typeparam name="T">the object type</typeparam>
        /// <param name="name"> the name</param>
        /// <param name="deleteOptions">the delete options</param>
        /// <param name="cancellationToken">the token </param>
        /// <returns>the kubernetes object</returns>
        public async Task <T> DeleteAsync <T>(string name, V1DeleteOptions deleteOptions, CancellationToken cancellationToken = default)
            where T : class, IKubernetesObject <V1ObjectMeta>
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            var resp = await _client.DeleteClusterCustomObjectWithHttpMessagesAsync(
                group : _apiGroup, version : _apiVersion, plural : _resourcePlural, name : name, body : deleteOptions, cancellationToken : cancellationToken).ConfigureAwait(false);

            return(KubernetesJson.Deserialize <T>(resp.Body.ToString()));
        }
Пример #10
0
        protected override LeaderElectionRecord GetLeaderElectionRecord(T obj)
        {
            var recordRawStringContent = obj.GetAnnotation(LeaderElectionRecordAnnotationKey);

            if (string.IsNullOrEmpty(recordRawStringContent))
            {
                return(new LeaderElectionRecord());
            }

            var record = KubernetesJson.Deserialize <LeaderElectionRecord>(recordRawStringContent);

            return(record);
        }
Пример #11
0
        public void Serialize()
        {
            {
                var v = 123;
                IntstrIntOrString intorstr = v;

                Assert.Equal("123", KubernetesJson.Serialize(intorstr));
            }

            {
                IntstrIntOrString intorstr = "12%";
                Assert.Equal("\"12%\"", KubernetesJson.Serialize(intorstr));
            }
        }
Пример #12
0
        /// <summary>
        /// List kubernetes object.
        /// </summary>
        /// <typeparam name="T">the object type</typeparam>
        /// <param name="listOptions">the list options</param>
        /// <param name="cancellationToken">the token </param>
        /// <returns>the kubernetes object</returns>
        public async Task <T> ListAsync <T>(ListOptions listOptions, CancellationToken cancellationToken = default)
            where T : class, IKubernetesObject <V1ListMeta>
        {
            if (listOptions == null)
            {
                throw new ArgumentNullException(nameof(listOptions));
            }

            var resp = await _client.ListClusterCustomObjectWithHttpMessagesAsync(group : _apiGroup, plural : _resourcePlural, version : _apiVersion, resourceVersion : listOptions.ResourceVersion,
                                                                                  continueParameter : listOptions.Continue, fieldSelector : listOptions.FieldSelector, labelSelector : listOptions.LabelSelector, limit : listOptions.Limit,
                                                                                  timeoutSeconds : listOptions.TimeoutSeconds, cancellationToken : cancellationToken).ConfigureAwait(false);

            return(KubernetesJson.Deserialize <T>(resp.Body.ToString()));
        }
Пример #13
0
        /// <summary>
        /// Get kubernetes object.
        /// </summary>
        /// <typeparam name="T">the object type</typeparam>
        /// <param name="namespaceProperty"> the namespaceProperty</param>
        /// <param name="name"> the name</param>
        /// <param name="getOptions">the get options</param>
        /// <param name="cancellationToken">the token </param>
        /// <returns>the kubernetes object</returns>
        public async Task <T> GetAsync <T>(string namespaceProperty, string name, GetOptions getOptions, CancellationToken cancellationToken = default)
            where T : class, IKubernetesObject <V1ObjectMeta>
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrEmpty(namespaceProperty))
            {
                throw new ArgumentNullException(nameof(namespaceProperty));
            }

            var resp = await _client.GetNamespacedCustomObjectWithHttpMessagesAsync(group : _apiGroup, plural : _resourcePlural, version : _apiVersion, name : name, namespaceParameter : namespaceProperty,
                                                                                    cancellationToken : cancellationToken).ConfigureAwait(false);

            return(KubernetesJson.Deserialize <T>(resp.Body.ToString()));
        }
        public async Task <bool> ShouldNext(HttpContext httpContext)
        {
            var isWatch          = (httpContext.Request.Query.ContainsKey("watch") && httpContext.Request.Query["watch"] == "true");
            var returnStatusCode = (_serverFlags.HasFlag(MockKubeServerFlags.Throw500) ? HttpStatusCode.InternalServerError : HttpStatusCode.OK);

            httpContext.Response.StatusCode    = (int)returnStatusCode;
            httpContext.Response.ContentLength = null;

            if (isWatch)
            {
                ServerShutdown = new AsyncManualResetEvent();

                foreach (Enum flag in Enum.GetValues(_serverFlags.GetType()))
                {
                    if (!_serverFlags.HasFlag(flag))
                    {
                        continue;
                    }

                    switch (flag)
                    {
                    case MockKubeServerFlags.AddedPod:
                        await WriteStreamLine(httpContext, _mockAddedEventStreamLine).ConfigureAwait(false);

                        break;

                    case MockKubeServerFlags.ErrorPod:
                        await WriteStreamLine(httpContext, _mockErrorStreamLine).ConfigureAwait(false);

                        break;

                    case MockKubeServerFlags.DeletedPod:
                        await WriteStreamLine(httpContext, _mockDeletedStreamLine).ConfigureAwait(false);

                        break;

                    case MockKubeServerFlags.ModifiedPod:
                        await WriteStreamLine(httpContext, _mockModifiedStreamLine).ConfigureAwait(false);

                        break;

                    case MockKubeServerFlags.BadJson:
                        await WriteStreamLine(httpContext, MockBadStreamLine).ConfigureAwait(false);

                        break;

                    case MockKubeServerFlags.Throw500:
                        return(false);
                    }
                }

                // keep server connection open
                await ServerShutdown.WaitAsync().ConfigureAwait(false);

                return(false);
            }

            foreach (Enum flag in Enum.GetValues(_serverFlags.GetType()))
            {
                if (!_serverFlags.HasFlag(flag))
                {
                    continue;
                }

                switch (flag)
                {
                case MockKubeServerFlags.ListPods:
                    await WriteStreamLine(httpContext, MockPodResponse).ConfigureAwait(false);

                    break;

                case MockKubeServerFlags.GetPod:
                    var corev1PodList = KubernetesJson.Deserialize <V1PodList>(MockPodResponse);
                    await WriteStreamLine(httpContext, KubernetesJson.Serialize(corev1PodList.Items.First())).ConfigureAwait(false);

                    break;

                case MockKubeServerFlags.Throw500:
                    return(false);
                }
            }

            return(false);
        }
Пример #15
0
 protected override T SetLeaderElectionRecord(LeaderElectionRecord record, T metaObj)
 {
     metaObj.SetAnnotation(LeaderElectionRecordAnnotationKey, KubernetesJson.Serialize(record));
     return(metaObj);
 }